example using multiprocessing in python
Example 1: multiprocessing join python
from multiprocessing import Process
def say_hello(name='world'):
print "Hello, %s" % name
p = Process(target=say_hello)
p.start()
p.join()
Example 2: worker pool model with multiprocessing
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))
Example 3: python multiprocessing
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))
Example 4: python - multiprocessing
import time
from multiprocessing import Process
def my_func_1():...
def my_func_2():...
start = time.time()
my_func_1()
my_func_2()
print(f'Single thread total time: {time.time() - start}')
process = Process(target=my_func_1)
process2 = Process(target=my_func_2)
process.start()
process2.start()
start = time.time()
process.join()
process2.join()
print(f'Two thread total time: {time.time() - start}')