Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
3bb8bad4
Commit
3bb8bad4
authored
Jun 18, 2012
by
Richard Oudkerk
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #15064: Use with-blocks for some examples in docs.
parent
c5d94e9b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
47 deletions
+39
-47
Doc/library/multiprocessing.rst
Doc/library/multiprocessing.rst
+39
-47
No files found.
Doc/library/multiprocessing.rst
View file @
3bb8bad4
...
...
@@ -241,17 +241,16 @@ 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))
d = manager.dict()
l = manager.list(range(10))
p = Process(target=f, args=(d, l))
p.start()
p.join()
p = Process(target=f, args=(d, l))
p.start()
p.join()
print(d)
print(l)
print(d)
print(l)
will print ::
...
...
@@ -279,10 +278,10 @@ For example::
return x*x
if __name__ == '
__main__
':
pool = Pool(processes=4)
# 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]"
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]"
Reference
...
...
@@ -1426,11 +1425,10 @@ callables with the manager class. For example::
MyManager
.
register
(
'Maths'
,
MathsClass
)
if
__name__
==
'__main__'
:
manager
=
MyManager
()
manager
.
start
()
maths
=
manager
.
Maths
()
print
(
maths
.
add
(
4
,
3
))
#
prints
7
print
(
maths
.
mul
(
7
,
8
))
#
prints
56
with
MyManager
()
as
manager
:
maths
=
manager
.
Maths
()
print
(
maths
.
add
(
4
,
3
))
#
prints
7
print
(
maths
.
mul
(
7
,
8
))
#
prints
56
Using
a
remote
manager
...
...
@@ -1798,21 +1796,20 @@ 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
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]"
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))
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.apply_async(time.sleep, (10,))
print(result.get(timeout=1)) # raises TimeoutError
import time
result = pool.apply_async(time.sleep, (10,))
print(result.get(timeout=1)) # raises TimeoutError
.. _multiprocessing-listeners-clients:
...
...
@@ -1984,19 +1981,16 @@ 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()
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()
listener.close()
conn.send_bytes(array('
i
', [42, 1729]))
The following code connects to the server and receives some data from the
server::
...
...
@@ -2005,17 +1999,15 @@ server::
from array import array
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_into(arr)) # => 8
print(arr) # => array('
i
', [42, 1729, 0, 0, 0])
print(conn.recv_bytes()) # => '
hello
'
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
wait for messages from multiple processes at once::
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment