Код: Выделить всё
import multiprocessing
import time
def isprime(num):
if num < 2:
return None
for i in range(2, num):
if (num % i) == 0:
return None
else:
return num
if __name__ == "__main__":
pool = multiprocessing.Pool(3)
start_time = time.perf_counter()
result = list(filter(lambda x: x is not None, pool.map(isprime, range(0,30))))
finish_time = time.perf_counter()
print(f"Program finished in {finish_time-start_time} seconds")
print(result)
Код: Выделить всё
from multiprocessing import Process, Pipe
def parentData(parent):
''' This function sends the data for the child process '''
parent.send(['Hello'])
parent.close()
def childData(child):
''' This function sends the data for the parent process '''
child.send(['Bye'])
child.close()
if __name__ == '__main__':
parent, child = Pipe() # Create Pipe
process1 = Process(target = parentData, args = (parent, )) # Create a process for handling parent data
process2 = Process(target = childData, args = (child, )) # Create a process for handling child data
process1.start() # Start the parent process
process2.start() # Start the child process
print(parent.recv()) # Display data received from child (BYE)
print(child.recv()) # Display data received from parent (HELLO)
process1.join() # Wait till the process completes its execution
process2.join()
Подробнее: https://stackoverflow.com/questions/721 ... n-interval
Мобильная версия