Hello, I am using Python as a jupyter notebook I have difficulty using multiprocessing module, I have a question.
Even if you turn the example code on several blogs, I don't know if there's something wrong with Jupiter' Maybe the code is weird, but there are a lot of print doors that don't come out properly.
import sys, random
People_number = input("Input number of peoplet: ")
People_number = int(People_number)
def sum():
sum = 0
while sum < 100:
number = random.randint(1,10)
sum = sum + number
return sum
print(sum())
First of all, the code being modified is the same as above,
The sum function is a random integer from 1 to 9 If the sum goes over 100, It is a structure that returns the sum value after exiting the while statement.
The code I want is I'll input People_number Let's say, four people Four people will run the sum function The number and value of the person who finished the function the fastest I want to create a function that returns...
Must be executable on anaconda Jupiter notebook. Python coding masters, please <
python jupyter multiprocessing
sum is not a valid variable name because it has a built-in function.
The existing sum function was used as much as possible, the multiprocessing part was added, and the sort was added. (Since it was sorted, the first one is the fastest value.))
from timeit import default_timer as timer
from multiprocessing import Pool
import random
import time
def _sum(to):
vsum = 0
start = timer()
while vsum < to:
number = random.randint(1,10)
vsum = vsum + number
time.sleep(0.1)
end = timer()
return (vsum, end - start)
p = Pool(4)
result = p.map(func=_sum, iterable=(100, 100, 100, 100))
print(sorted(result, key=lambda item:item[1]))
© 2024 OneMinuteCode. All rights reserved.