Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 633c4d9

Browse files
committed
Issue #15064: Use with-blocks for some examples in docs.
1 parent ac38571 commit 633c4d9

1 file changed

Lines changed: 39 additions & 47 deletions

File tree

Doc/library/multiprocessing.rst

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,16 @@ However, if you really do need to use some shared data then
241241
l.reverse()
242242

243243
if __name__ == '__main__':
244-
manager = Manager()
244+
with Manager() as manager:
245+
d = manager.dict()
246+
l = manager.list(range(10))
245247

246-
d = manager.dict()
247-
l = manager.list(range(10))
248+
p = Process(target=f, args=(d, l))
249+
p.start()
250+
p.join()
248251

249-
p = Process(target=f, args=(d, l))
250-
p.start()
251-
p.join()
252-
253-
print(d)
254-
print(l)
252+
print(d)
253+
print(l)
255254

256255
will print ::
257256

@@ -279,10 +278,10 @@ For example::
279278
return x*x
280279

281280
if __name__ == '__main__':
282-
pool = Pool(processes=4) # start 4 worker processes
283-
result = pool.apply_async(f, [10]) # evaluate "f(10)" asynchronously
284-
print(result.get(timeout=1)) # prints "100" unless your computer is *very* slow
285-
print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]"
281+
with Pool(processes=4) as pool # start 4 worker processes
282+
result = pool.apply_async(f, [10]) # evaluate "f(10)" asynchronously
283+
print(result.get(timeout=1)) # prints "100" unless your computer is *very* slow
284+
print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]"
286285

287286

288287
Reference
@@ -1426,11 +1425,10 @@ callables with the manager class. For example::
14261425
MyManager.register('Maths', MathsClass)
14271426

14281427
if __name__ == '__main__':
1429-
manager = MyManager()
1430-
manager.start()
1431-
maths = manager.Maths()
1432-
print(maths.add(4, 3)) # prints 7
1433-
print(maths.mul(7, 8)) # prints 56
1428+
with MyManager() as manager:
1429+
maths = manager.Maths()
1430+
print(maths.add(4, 3)) # prints 7
1431+
print(maths.mul(7, 8)) # prints 56
14341432

14351433

14361434
Using a remote manager
@@ -1798,21 +1796,20 @@ The following example demonstrates the use of a pool::
17981796
return x*x
17991797

18001798
if __name__ == '__main__':
1801-
pool = Pool(processes=4) # start 4 worker processes
1799+
with Pool(processes=4) as pool: # start 4 worker processes
1800+
result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously
1801+
print(result.get(timeout=1)) # prints "100" unless your computer is *very* slow
18021802

1803-
result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously
1804-
print(result.get(timeout=1)) # prints "100" unless your computer is *very* slow
1803+
print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]"
18051804

1806-
print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]"
1805+
it = pool.imap(f, range(10))
1806+
print(next(it)) # prints "0"
1807+
print(next(it)) # prints "1"
1808+
print(it.next(timeout=1)) # prints "4" unless your computer is *very* slow
18071809

1808-
it = pool.imap(f, range(10))
1809-
print(next(it)) # prints "0"
1810-
print(next(it)) # prints "1"
1811-
print(it.next(timeout=1)) # prints "4" unless your computer is *very* slow
1812-
1813-
import time
1814-
result = pool.apply_async(time.sleep, (10,))
1815-
print(result.get(timeout=1)) # raises TimeoutError
1810+
import time
1811+
result = pool.apply_async(time.sleep, (10,))
1812+
print(result.get(timeout=1)) # raises TimeoutError
18161813

18171814

18181815
.. _multiprocessing-listeners-clients:
@@ -1984,19 +1981,16 @@ the client::
19841981
from array import array
19851982

19861983
address = ('localhost', 6000) # family is deduced to be 'AF_INET'
1987-
listener = Listener(address, authkey=b'secret password')
1988-
1989-
conn = listener.accept()
1990-
print('connection accepted from', listener.last_accepted)
19911984

1992-
conn.send([2.25, None, 'junk', float])
1985+
with Listener(address, authkey=b'secret password') as listener:
1986+
with listener.accept() as conn:
1987+
print('connection accepted from', listener.last_accepted)
19931988

1994-
conn.send_bytes(b'hello')
1989+
conn.send([2.25, None, 'junk', float])
19951990

1996-
conn.send_bytes(array('i', [42, 1729]))
1991+
conn.send_bytes(b'hello')
19971992

1998-
conn.close()
1999-
listener.close()
1993+
conn.send_bytes(array('i', [42, 1729]))
20001994

20011995
The following code connects to the server and receives some data from the
20021996
server::
@@ -2005,17 +1999,15 @@ server::
20051999
from array import array
20062000

20072001
address = ('localhost', 6000)
2008-
conn = Client(address, authkey=b'secret password')
2009-
2010-
print(conn.recv()) # => [2.25, None, 'junk', float]
20112002

2012-
print(conn.recv_bytes()) # => 'hello'
2003+
with Client(address, authkey=b'secret password') as conn:
2004+
print(conn.recv()) # => [2.25, None, 'junk', float]
20132005

2014-
arr = array('i', [0, 0, 0, 0, 0])
2015-
print(conn.recv_bytes_into(arr)) # => 8
2016-
print(arr) # => array('i', [42, 1729, 0, 0, 0])
2006+
print(conn.recv_bytes()) # => 'hello'
20172007

2018-
conn.close()
2008+
arr = array('i', [0, 0, 0, 0, 0])
2009+
print(conn.recv_bytes_into(arr)) # => 8
2010+
print(arr) # => array('i', [42, 1729, 0, 0, 0])
20192011

20202012
The following code uses :func:`~multiprocessing.connection.wait` to
20212013
wait for messages from multiple processes at once::

0 commit comments

Comments
 (0)