from multiprocessing import Pool
Python Multiprocessing
Pool
Pool??
Signature: Pool(processes=None, initializer=None, initargs=(), maxtasksperchild=None) Source: def Pool(self, processes=None, initializer=None, initargs=(), maxtasksperchild=None): '''Returns a process pool object''' from .pool import Pool return Pool(processes, initializer, initargs, maxtasksperchild, context=self.get_context()) File: ~/mambaforge/envs/cfast/lib/python3.11/multiprocessing/context.py Type: method
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))
[1, 4, 9]
Process
from multiprocessing import Process
import os
def info(title):
print(title)
print('module name:', __name__)
print('parent process:', os.getppid())
print('process id:', os.getpid())
def f(name):
'function f')
info(print('hello', name)
if __name__ == '__main__':
'main line')
info(= Process(target=f, args=('bob',))
p
p.start() p.join()
main line
module name: __main__
parent process: 2121
process id: 50591
function f
module name: __main__
parent process: 50591
process id: 50750
hello bob
Queues
from multiprocessing import Process, Queue
def f(q):
42, None, 'hello'])
q.put([
if __name__ == '__main__':
= Queue()
q = Process(target=f, args=(q,))
p
p.start()print(q.get()) # prints "[42, None, 'hello']"
p.join()
[42, None, 'hello']
Pipes
from multiprocessing import Process, Pipe
def f(conn):
42, None, 'hello'])
conn.send([
conn.close()
if __name__ == '__main__':
= Pipe()
parent_conn, child_conn = Process(target=f, args=(child_conn,))
p
p.start()print(parent_conn.recv()) # prints "[42, None, 'hello']"
p.join()
[42, None, 'hello']
Lock
from multiprocessing import Process, Lock
def f(l, i):
l.acquire()try:
print('hello world', i)
finally:
l.release()
if __name__ == '__main__':
= Lock()
lock
for num in range(10):
=f, args=(lock, num)).start() Process(target
hello world
0hello world 1
hello world2
hello world3
hello world 4
hello world 5
hello world 6
hello world 7
hello world 8
hello world 9
Server process
from multiprocessing import Process, Manager
def f(d, l):
1] = '1'
d['2'] = 2
d[0.25] = None
d[
l.reverse()
if __name__ == '__main__':
with Manager() as manager:
= manager.dict()
d = manager.list(range(10))
l
= Process(target=f, args=(d, l))
p
p.start()
p.join()
print(d)
print(l)
{1: '1', '2': 2, 0.25: None}
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]