Commit 633c4d91 authored by Richard Oudkerk's avatar Richard Oudkerk

Issue #15064: Use with-blocks for some examples in docs.

parent ac38571f
...@@ -241,17 +241,16 @@ However, if you really do need to use some shared data then ...@@ -241,17 +241,16 @@ However, if you really do need to use some shared data then
l.reverse() l.reverse()
if __name__ == '__main__': if __name__ == '__main__':
manager = Manager() with Manager() as manager:
d = manager.dict()
l = manager.list(range(10))
d = manager.dict() p = Process(target=f, args=(d, l))
l = manager.list(range(10)) p.start()
p.join()
p = Process(target=f, args=(d, l)) print(d)
p.start() print(l)
p.join()
print(d)
print(l)
will print :: will print ::
...@@ -279,10 +278,10 @@ For example:: ...@@ -279,10 +278,10 @@ For example::
return x*x return x*x
if __name__ == '__main__': if __name__ == '__main__':
pool = Pool(processes=4) # start 4 worker processes with Pool(processes=4) as pool # start 4 worker processes
result = pool.apply_async(f, [10]) # evaluate "f(10)" asynchronously result = pool.apply_async(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]"
Reference Reference
...@@ -1426,11 +1425,10 @@ callables with the manager class. For example:: ...@@ -1426,11 +1425,10 @@ callables with the manager class. For example::
MyManager.register('Maths', MathsClass) MyManager.register('Maths', MathsClass)
if __name__ == '__main__': if __name__ == '__main__':
manager = MyManager() with MyManager() as manager:
manager.start() maths = manager.Maths()
maths = manager.Maths() print(maths.add(4, 3)) # prints 7
print(maths.add(4, 3)) # prints 7 print(maths.mul(7, 8)) # prints 56
print(maths.mul(7, 8)) # prints 56
Using a remote manager Using a remote manager
...@@ -1798,21 +1796,20 @@ The following example demonstrates the use of a pool:: ...@@ -1798,21 +1796,20 @@ The following example demonstrates the use of a pool::
return x*x return x*x
if __name__ == '__main__': if __name__ == '__main__':
pool = Pool(processes=4) # start 4 worker processes with Pool(processes=4) as pool: # start 4 worker processes
result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously
print(result.get(timeout=1)) # prints "100" unless your computer is *very* slow
result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]"
print(result.get(timeout=1)) # prints "100" unless your computer is *very* slow
print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]" it = pool.imap(f, range(10))
print(next(it)) # prints "0"
print(next(it)) # prints "1"
print(it.next(timeout=1)) # prints "4" unless your computer is *very* slow
it = pool.imap(f, range(10)) import time
print(next(it)) # prints "0" result = pool.apply_async(time.sleep, (10,))
print(next(it)) # prints "1" print(result.get(timeout=1)) # raises TimeoutError
print(it.next(timeout=1)) # prints "4" unless your computer is *very* slow
import time
result = pool.apply_async(time.sleep, (10,))
print(result.get(timeout=1)) # raises TimeoutError
.. _multiprocessing-listeners-clients: .. _multiprocessing-listeners-clients:
...@@ -1984,19 +1981,16 @@ the client:: ...@@ -1984,19 +1981,16 @@ the client::
from array import array from array import array
address = ('localhost', 6000) # family is deduced to be 'AF_INET' address = ('localhost', 6000) # family is deduced to be 'AF_INET'
listener = Listener(address, authkey=b'secret password')
conn = listener.accept()
print('connection accepted from', listener.last_accepted)
conn.send([2.25, None, 'junk', float]) with Listener(address, authkey=b'secret password') as listener:
with listener.accept() as conn:
print('connection accepted from', listener.last_accepted)
conn.send_bytes(b'hello') conn.send([2.25, None, 'junk', float])
conn.send_bytes(array('i', [42, 1729])) conn.send_bytes(b'hello')
conn.close() conn.send_bytes(array('i', [42, 1729]))
listener.close()
The following code connects to the server and receives some data from the The following code connects to the server and receives some data from the
server:: server::
...@@ -2005,17 +1999,15 @@ server:: ...@@ -2005,17 +1999,15 @@ server::
from array import array from array import array
address = ('localhost', 6000) address = ('localhost', 6000)
conn = Client(address, authkey=b'secret password')
print(conn.recv()) # => [2.25, None, 'junk', float]
print(conn.recv_bytes()) # => 'hello' with Client(address, authkey=b'secret password') as conn:
print(conn.recv()) # => [2.25, None, 'junk', float]
arr = array('i', [0, 0, 0, 0, 0]) print(conn.recv_bytes()) # => 'hello'
print(conn.recv_bytes_into(arr)) # => 8
print(arr) # => array('i', [42, 1729, 0, 0, 0])
conn.close() arr = array('i', [0, 0, 0, 0, 0])
print(conn.recv_bytes_into(arr)) # => 8
print(arr) # => array('i', [42, 1729, 0, 0, 0])
The following code uses :func:`~multiprocessing.connection.wait` to The following code uses :func:`~multiprocessing.connection.wait` to
wait for messages from multiple processes at once:: wait for messages from multiple processes at once::
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment