Commit 99e93d2d authored by Petri Lehtinen's avatar Petri Lehtinen

merge heads

parents 8ffbab8d 46f990e5
...@@ -123,7 +123,9 @@ and off individually. They are described here in more detail. ...@@ -123,7 +123,9 @@ and off individually. They are described here in more detail.
.. 2to3fixer:: callable .. 2to3fixer:: callable
Converts ``callable(x)`` to ``isinstance(x, collections.Callable)``, adding Converts ``callable(x)`` to ``isinstance(x, collections.Callable)``, adding
an import to :mod:`collections` if needed. an import to :mod:`collections` if needed. Note ``callable(x)`` has returned
in Python 3.2, so if you do not intend to support Python 3.1, you can disable
this fixer.
.. 2to3fixer:: dict .. 2to3fixer:: dict
......
...@@ -225,6 +225,7 @@ The server classes support the following class variables: ...@@ -225,6 +225,7 @@ The server classes support the following class variables:
desired. If :meth:`handle_request` receives no incoming requests within the desired. If :meth:`handle_request` receives no incoming requests within the
timeout period, the :meth:`handle_timeout` method is called. timeout period, the :meth:`handle_timeout` method is called.
There are various server methods that can be overridden by subclasses of base There are various server methods that can be overridden by subclasses of base
server classes like :class:`TCPServer`; these methods aren't useful to external server classes like :class:`TCPServer`; these methods aren't useful to external
users of the server object. users of the server object.
...@@ -355,7 +356,7 @@ This is the server side:: ...@@ -355,7 +356,7 @@ This is the server side::
def handle(self): def handle(self):
# self.request is the TCP socket connected to the client # self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip() self.data = self.request.recv(1024).strip()
print "%s wrote:" % self.client_address[0] print "{} wrote:".format(self.client_address[0])
print self.data print self.data
# just send back the same data, but upper-cased # just send back the same data, but upper-cased
self.request.send(self.data.upper()) self.request.send(self.data.upper())
...@@ -379,7 +380,7 @@ objects that simplify communication by providing the standard file interface):: ...@@ -379,7 +380,7 @@ objects that simplify communication by providing the standard file interface)::
# self.rfile is a file-like object created by the handler; # self.rfile is a file-like object created by the handler;
# we can now use e.g. readline() instead of raw recv() calls # we can now use e.g. readline() instead of raw recv() calls
self.data = self.rfile.readline().strip() self.data = self.rfile.readline().strip()
print "%s wrote:" % self.client_address[0] print "{} wrote:".format(self.client_address[0])
print self.data print self.data
# Likewise, self.wfile is a file-like object used to write back # Likewise, self.wfile is a file-like object used to write back
# to the client # to the client
...@@ -402,16 +403,18 @@ This is the client side:: ...@@ -402,16 +403,18 @@ This is the client side::
# Create a socket (SOCK_STREAM means a TCP socket) # Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to server and send data try:
sock.connect((HOST, PORT)) # Connect to server and send data
sock.send(data + "\n") sock.connect((HOST, PORT))
sock.send(data + "\n")
# Receive data from the server and shut down # Receive data from the server and shut down
received = sock.recv(1024) received = sock.recv(1024)
sock.close() finally:
sock.close()
print "Sent: %s" % data print "Sent: {}".format(data)
print "Received: %s" % received print "Received: {}".format(received)
The output of the example should look something like this: The output of the example should look something like this:
...@@ -452,7 +455,7 @@ This is the server side:: ...@@ -452,7 +455,7 @@ This is the server side::
def handle(self): def handle(self):
data = self.request[0].strip() data = self.request[0].strip()
socket = self.request[1] socket = self.request[1]
print "%s wrote:" % self.client_address[0] print "{} wrote:".format(self.client_address[0])
print data print data
socket.sendto(data.upper(), self.client_address) socket.sendto(data.upper(), self.client_address)
...@@ -477,8 +480,8 @@ This is the client side:: ...@@ -477,8 +480,8 @@ This is the client side::
sock.sendto(data + "\n", (HOST, PORT)) sock.sendto(data + "\n", (HOST, PORT))
received = sock.recv(1024) received = sock.recv(1024)
print "Sent: %s" % data print "Sent: {}".format(data)
print "Received: %s" % received print "Received: {}".format(received)
The output of the example should look exactly like for the TCP server example. The output of the example should look exactly like for the TCP server example.
...@@ -499,8 +502,8 @@ An example for the :class:`ThreadingMixIn` class:: ...@@ -499,8 +502,8 @@ An example for the :class:`ThreadingMixIn` class::
def handle(self): def handle(self):
data = self.request.recv(1024) data = self.request.recv(1024)
cur_thread = threading.currentThread() cur_thread = threading.current_thread()
response = "%s: %s" % (cur_thread.getName(), data) response = "{}: {}".format(cur_thread.name, data)
self.request.send(response) self.request.send(response)
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
...@@ -509,10 +512,12 @@ An example for the :class:`ThreadingMixIn` class:: ...@@ -509,10 +512,12 @@ An example for the :class:`ThreadingMixIn` class::
def client(ip, port, message): def client(ip, port, message):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port)) sock.connect((ip, port))
sock.send(message) try:
response = sock.recv(1024) sock.send(message)
print "Received: %s" % response response = sock.recv(1024)
sock.close() print "Received: {}".format(response)
finally:
sock.close()
if __name__ == "__main__": if __name__ == "__main__":
# Port 0 means to select an arbitrary unused port # Port 0 means to select an arbitrary unused port
...@@ -525,9 +530,9 @@ An example for the :class:`ThreadingMixIn` class:: ...@@ -525,9 +530,9 @@ An example for the :class:`ThreadingMixIn` class::
# more thread for each request # more thread for each request
server_thread = threading.Thread(target=server.serve_forever) server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates # Exit the server thread when the main thread terminates
server_thread.setDaemon(True) server_thread.daemon = True
server_thread.start() server_thread.start()
print "Server loop running in thread:", server_thread.getName() print "Server loop running in thread:", server_thread.name
client(ip, port, "Hello World 1") client(ip, port, "Hello World 1")
client(ip, port, "Hello World 2") client(ip, port, "Hello World 2")
...@@ -535,6 +540,7 @@ An example for the :class:`ThreadingMixIn` class:: ...@@ -535,6 +540,7 @@ An example for the :class:`ThreadingMixIn` class::
server.shutdown() server.shutdown()
The output of the example should look something like this:: The output of the example should look something like this::
$ python ThreadedTCPServer.py $ python ThreadedTCPServer.py
......
This diff is collapsed.
...@@ -294,7 +294,11 @@ class Pool(object): ...@@ -294,7 +294,11 @@ class Pool(object):
@staticmethod @staticmethod
def _handle_workers(pool): def _handle_workers(pool):
while pool._worker_handler._state == RUN and pool._state == RUN: thread = threading.current_thread()
# Keep maintaining workers until the cache gets drained, unless the pool
# is terminated.
while thread._state == RUN or (pool._cache and thread._state != TERMINATE):
pool._maintain_pool() pool._maintain_pool()
time.sleep(0.1) time.sleep(0.1)
# send sentinel to stop workers # send sentinel to stop workers
......
...@@ -1168,6 +1168,20 @@ class _TestPoolWorkerLifetime(BaseTestCase): ...@@ -1168,6 +1168,20 @@ class _TestPoolWorkerLifetime(BaseTestCase):
p.close() p.close()
p.join() p.join()
def test_pool_worker_lifetime_early_close(self):
# Issue #10332: closing a pool whose workers have limited lifetimes
# before all the tasks completed would make join() hang.
p = multiprocessing.Pool(3, maxtasksperchild=1)
results = []
for i in range(6):
results.append(p.apply_async(sqr, (i, 0.3)))
p.close()
p.join()
# check the results
for (j, res) in enumerate(results):
self.assertEqual(res.get(), sqr(j))
# #
# Test that manager has expected number of shared objects left # Test that manager has expected number of shared objects left
# #
......
...@@ -69,6 +69,9 @@ Core and Builtins ...@@ -69,6 +69,9 @@ Core and Builtins
Library Library
------- -------
- Issue #10332: multiprocessing: fix a race condition when a Pool is closed
before all tasks have completed.
- Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode - Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
arguments with the system default encoding just like the write() method arguments with the system default encoding just like the write() method
does, instead of converting it to a raw buffer. This also fixes handling of does, instead of converting it to a raw buffer. This also fixes handling of
...@@ -345,6 +348,14 @@ Tests ...@@ -345,6 +348,14 @@ Tests
- Issue #12057: Add tests for ISO 2022 codecs (iso2022_jp, iso2022_jp_2, - Issue #12057: Add tests for ISO 2022 codecs (iso2022_jp, iso2022_jp_2,
iso2022_kr). iso2022_kr).
Documentation
-------------
- Issue #13237: Reorganise subprocess documentation to emphasise convenience
functions and the most commonly needed arguments to Popen.
- Issue #13141: Demonstrate recommended style for SocketServer examples.
What's New in Python 2.7.2? What's New in Python 2.7.2?
=========================== ===========================
......
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