Kaydet (Commit) 633c4d91 authored tarafından Richard Oudkerk's avatar Richard Oudkerk

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

üst ac38571f
...@@ -241,8 +241,7 @@ However, if you really do need to use some shared data then ...@@ -241,8 +241,7 @@ 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() d = manager.dict()
l = manager.list(range(10)) l = manager.list(range(10))
...@@ -279,7 +278,7 @@ For example:: ...@@ -279,7 +278,7 @@ 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]"
...@@ -1426,8 +1425,7 @@ callables with the manager class. For example:: ...@@ -1426,8 +1425,7 @@ 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
...@@ -1798,8 +1796,7 @@ The following example demonstrates the use of a pool:: ...@@ -1798,8 +1796,7 @@ 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 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
...@@ -1984,9 +1981,9 @@ the client:: ...@@ -1984,9 +1981,9 @@ 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() with Listener(address, authkey=b'secret password') as listener:
with listener.accept() as conn:
print('connection accepted from', listener.last_accepted) print('connection accepted from', listener.last_accepted)
conn.send([2.25, None, 'junk', float]) conn.send([2.25, None, 'junk', float])
...@@ -1995,9 +1992,6 @@ the client:: ...@@ -1995,9 +1992,6 @@ the client::
conn.send_bytes(array('i', [42, 1729])) conn.send_bytes(array('i', [42, 1729]))
conn.close()
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,8 +1999,8 @@ server:: ...@@ -2005,8 +1999,8 @@ server::
from array import array from array import array
address = ('localhost', 6000) 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()) # => [2.25, None, 'junk', float]
print(conn.recv_bytes()) # => 'hello' print(conn.recv_bytes()) # => 'hello'
...@@ -2015,8 +2009,6 @@ server:: ...@@ -2015,8 +2009,6 @@ server::
print(conn.recv_bytes_into(arr)) # => 8 print(conn.recv_bytes_into(arr)) # => 8
print(arr) # => array('i', [42, 1729, 0, 0, 0]) print(arr) # => array('i', [42, 1729, 0, 0, 0])
conn.close()
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