Skip to content

Commit 5ce513a

Browse files
committed
wsgimp example: make child_msg_generator picklable
this is to fix: AttributeError: Can't pickle local object 'main.<locals>.child_msg_generator' (and the comparable error on PyPy)
1 parent 3507d59 commit 5ce513a

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

examples/wsgimultiprocessing.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,25 @@
4545
N_HTTP_CLIENTS = 100
4646

4747

48-
def main():
48+
def child_msg_generator(pipewriter):
49+
"""I am executed in a child process.
4950
50-
def servelet(http_server):
51-
"""I am executed in a greenlet.
51+
I write some dummy payload to the write end of the pipe through which I
52+
am connected to my parent process. I terminate immediately after writing
53+
the message.
54+
"""
55+
pipewriter.put(DUMMY_PAYLOAD)
5256

53-
It is my job to hang in the cooperatively blocking `serve_forever()`
54-
call to accept incoming connections. I only terminate when I am
55-
explicitly killed from the outside.
56-
"""
57-
http_server.serve_forever()
57+
def invoke_server_forever(http_server):
58+
"""I am executed in a greenlet.
5859
59-
def child_msg_generator(pipewriter):
60-
"""I am executed in a child process.
60+
It is my job to hang in the cooperatively blocking `serve_forever()`
61+
call to accept incoming connections. I only terminate when I am
62+
explicitly killed from the outside.
63+
"""
64+
http_server.serve_forever()
6165

62-
I write some dummy payload to the write end of the pipe through which I
63-
am connected to my parent process. I terminate immediately after writing
64-
the message.
65-
"""
66-
pipewriter.put(DUMMY_PAYLOAD)
66+
def main():
6767

6868
def handle_http_request(_, start_response):
6969
"""I am executed in a greenlet whenever an HTTP request came in."""
@@ -92,7 +92,7 @@ def handle_http_request(_, start_response):
9292
return [body]
9393

9494
server = WSGIServer(('127.0.0.1', 0), handle_http_request, log=None)
95-
servelet = gevent.spawn(servelet, server)
95+
servelet = gevent.spawn(invoke_server_forever, server)
9696

9797
# Wait for server to be bound to socket.
9898
while True:
@@ -131,6 +131,7 @@ def child_client_runner(server_address):
131131
"""
132132

133133
def get():
134+
# Expected to throw an HTTPError when the response code is not 200.
134135
body = request.urlopen('http://%s:%s/' % server_address).read()
135136
assert body == DUMMY_PAYLOAD
136137

0 commit comments

Comments
 (0)