Issue #15064: Use with-blocks for some examples in docs.
This commit is contained in:
parent
ac38571f00
commit
633c4d9199
|
@ -241,8 +241,7 @@ However, if you really do need to use some shared data then
|
|||
l.reverse()
|
||||
|
||||
if __name__ == '__main__':
|
||||
manager = Manager()
|
||||
|
||||
with Manager() as manager:
|
||||
d = manager.dict()
|
||||
l = manager.list(range(10))
|
||||
|
||||
|
@ -279,7 +278,7 @@ For example::
|
|||
return x*x
|
||||
|
||||
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
|
||||
print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]"
|
||||
|
@ -1426,8 +1425,7 @@ callables with the manager class. For example::
|
|||
MyManager.register('Maths', MathsClass)
|
||||
|
||||
if __name__ == '__main__':
|
||||
manager = MyManager()
|
||||
manager.start()
|
||||
with MyManager() as manager:
|
||||
maths = manager.Maths()
|
||||
print(maths.add(4, 3)) # prints 7
|
||||
print(maths.mul(7, 8)) # prints 56
|
||||
|
@ -1798,8 +1796,7 @@ The following example demonstrates the use of a pool::
|
|||
return x*x
|
||||
|
||||
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
|
||||
|
||||
|
@ -1984,9 +1981,9 @@ the client::
|
|||
from array import array
|
||||
|
||||
address = ('localhost', 6000) # family is deduced to be 'AF_INET'
|
||||
listener = Listener(address, authkey=b'secret password')
|
||||
|
||||
conn = listener.accept()
|
||||
with Listener(address, authkey=b'secret password') as listener:
|
||||
with listener.accept() as conn:
|
||||
print('connection accepted from', listener.last_accepted)
|
||||
|
||||
conn.send([2.25, None, 'junk', float])
|
||||
|
@ -1995,9 +1992,6 @@ the client::
|
|||
|
||||
conn.send_bytes(array('i', [42, 1729]))
|
||||
|
||||
conn.close()
|
||||
listener.close()
|
||||
|
||||
The following code connects to the server and receives some data from the
|
||||
server::
|
||||
|
||||
|
@ -2005,8 +1999,8 @@ server::
|
|||
from array import array
|
||||
|
||||
address = ('localhost', 6000)
|
||||
conn = Client(address, authkey=b'secret password')
|
||||
|
||||
with Client(address, authkey=b'secret password') as conn:
|
||||
print(conn.recv()) # => [2.25, None, 'junk', float]
|
||||
|
||||
print(conn.recv_bytes()) # => 'hello'
|
||||
|
@ -2015,8 +2009,6 @@ server::
|
|||
print(conn.recv_bytes_into(arr)) # => 8
|
||||
print(arr) # => array('i', [42, 1729, 0, 0, 0])
|
||||
|
||||
conn.close()
|
||||
|
||||
The following code uses :func:`~multiprocessing.connection.wait` to
|
||||
wait for messages from multiple processes at once::
|
||||
|
||||
|
|
Loading…
Reference in New Issue