Introduction
-----------------------
+------------
:mod:`multiprocessing` is a package that supports spawning processes using an
API similar to the :mod:`threading` module. The :mod:`multiprocessing` package
from multiprocessing import Process
def f(name):
- print 'hello', name
+ print('hello', name)
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
q = Queue()
p = Process(target=f, args=(q,))
p.start()
- print q.get() # prints "[42, None, 'hello']"
+ print(q.get()) # prints "[42, None, 'hello']"
p.join()
Queues are thread and process safe.
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,))
p.start()
- print parent_conn.recv() # prints "[42, None, 'hello']"
+ print(parent_conn.recv()) # prints "[42, None, 'hello']"
p.join()
The two connection objects returned by :func:`Pipe` represent the two ends of
def f(l, i):
l.acquire()
- print 'hello world', i
+ print('hello world', i)
l.release()
if __name__ == '__main__':
p.start()
p.join()
- print num.value
- print arr[:]
+ print(num.value)
+ print(arr[:])
will print ::
p.start()
p.join()
- print d
- print l
+ print(d)
+ print(l)
will print ::
return x*x
if __name__ == '__main__':
- pool = Pool(processes=4) # start 4 worker processes
- result = pool.applyAsync(f, [10]) # evaluate "f(10)" asynchronously
- print result.get(timeout=1) # prints "100" unless your computer is *very* slow
- print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]"
+ pool = Pool(processes=4) # start 4 worker processes
+ result = pool.applyAsync(f, [10]) # evaluate "f(10)" asynchronously
+ print(result.get(timeout=1)) # prints "100" unless your computer is *very* slow
+ print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]"
Reference
>>> import processing, time, signal
>>> p = processing.Process(target=time.sleep, args=(1000,))
- >>> print p, p.is_alive()
+ >>> print(p, p.is_alive())
<Process(Process-1, initial)> False
>>> p.start()
- >>> print p, p.is_alive()
+ >>> print(p, p.is_alive())
<Process(Process-1, started)> True
>>> p.terminate()
- >>> print p, p.is_alive()
+ >>> print(p, p.is_alive())
<Process(Process-1, stopped[SIGTERM])> False
>>> p.exitcode == -signal.SIGTERM
True
from multiprocessing import Process, freeze_support
def f():
- print 'hello world!'
+ print('hello world!')
if __name__ == '__main__':
freeze_support()
p.start()
p.join()
- print n.value
- print x.value
- print s.value
- print [(a.x, a.y) for a in A]
+ print(n.value)
+ print(x.value)
+ print(s.value)
+ print([(a.x, a.y) for a in A])
-.. highlightlang:: none
+.. highlight:: none
The results printed are ::
HELLO WORLD
[(3.515625, 39.0625), (33.0625, 4.0), (5.640625, 90.25)]
-.. highlightlang:: python
+.. highlight:: python
.. _multiprocessing-managers:
>>> Global.x = 10
>>> Global.y = 'hello'
>>> Global._z = 12.3 # this is an attribute of the proxy
- >>> print Global
+ >>> print(Global)
Namespace(x=10, y='hello')
manager = MyManager()
manager.start()
maths = manager.Maths()
- print maths.add(4, 3) # prints 7
- print maths.mul(7, 8) # prints 56
+ print(maths.add(4, 3)) # prints 7
+ print(maths.mul(7, 8)) # prints 56
Using a remote manager
>>> from multiprocessing import Manager
>>> manager = Manager()
>>> l = manager.list([i*i for i in range(10)])
- >>> print l
+ >>> print(l)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
- >>> print repr(l)
+ >>> print(repr(l))
<ListProxy object, typeid 'list' at 0xb799974c>
>>> l[4]
16
>>> a = manager.list()
>>> b = manager.list()
>>> a.append(b) # referent of a now contains referent of b
- >>> print a, b
+ >>> print(a, b)
[[]] []
>>> b.append('hello')
- >>> print a, b
+ >>> print(a, b)
[['hello']] ['hello']
.. note::
pool = Pool(processes=4) # start 4 worker processes
result = pool.applyAsync(f, (10,)) # evaluate "f(10)" asynchronously
- print result.get(timeout=1) # prints "100" unless your computer is *very* slow
+ print(result.get(timeout=1)) # prints "100" unless your computer is *very* slow
- print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]"
+ print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]"
it = pool.imap(f, range(10))
- print it.next() # prints "0"
- print it.next() # prints "1"
- print it.next(timeout=1) # prints "4" unless your computer is *very* slow
+ print(next(it)) # prints "0"
+ print(next(it)) # prints "1"
+ print(it.next(timeout=1)) # prints "4" unless your computer is *very* slow
import time
result = pool.applyAsync(time.sleep, (10,))
- print result.get(timeout=1) # raises TimeoutError
+ print(result.get(timeout=1)) # raises TimeoutError
.. _multiprocessing-listeners-clients:
listener = Listener(address, authkey='secret password')
conn = listener.accept()
- print 'connection accepted from', listener.last_accepted
+ print('connection accepted from', listener.last_accepted)
conn.send([2.25, None, 'junk', float])
address = ('localhost', 6000)
conn = Client(address, authkey='secret password')
- print conn.recv() # => [2.25, None, 'junk', float]
+ print(conn.recv()) # => [2.25, None, 'junk', float]
- print conn.recv_bytes() # => 'hello'
+ print(conn.recv_bytes()) # => 'hello'
arr = array('i', [0, 0, 0, 0, 0])
- print conn.recv_bytes_into(arr) # => 8
- print arr # => array('i', [42, 1729, 0, 0, 0])
+ print(conn.recv_bytes_into(arr)) # => 8
+ print(arr) # => array('i', [42, 1729, 0, 0, 0])
conn.close()
from multiprocessing import Process
def foo():
- print 'hello'
+ print('hello')
p = Process(target=foo)
p.start()
from multiprocessing import Process, freeze_support
def foo():
- print 'hello'
+ print('hello')
if __name__ == '__main__':
freeze_support()