+# Copyright 2013, Jan-Philip Gehrcke. See LICENSE for details.
+from gevent.wsgi import WSGIServer
+Multiple clients (running in greenlets that concurrently run in a child
+process) request a response from a WSGIServer running in the parent. Each
+response is generated by a child process, i.e. each client connection makes the server process spawn a child.
+A greenlet in the parent, the `servelet`, runs the WSGIServer. Each client
+connection is handled in its own greenlet running the `hello_world` function.
+This delegates the response creation to a child process. The child process
+transfers the response back to the greenlet in the parent process via a gipc
+pipe. This yields the response to the client.
+Each client greenlet validates the response and terminates.
+Output on my test system: 100 clients were served within 0.43 s.
+ http_server = WSGIServer(('localhost', 0), hello_world, log=False)
+ servelet = gevent.spawn(serve, http_server)
+ # Wait for server being bound to socket.
+ if http_server.address != 0:
+ client = gipc.start_process(
+ args=(http_server.address, ))
+def hello_world(environ, start_response):
+ # Generate response in child process.
+ with gipc.pipe() as (reader, writer):
+ start_response('200 OK', [('Content-Type', 'text/html')])
+ rg = gipc.start_process(
+ response = reader.get()
+ assert urllib2.urlopen("http://%s:%s/" % server_address).read() == MSG
+ clientlets = [gevent.spawn(get) for _ in xrange(N)]
+ duration = time.time() - t1
+ print "%s clients were served within %.2f s." % (N, duration)
+if __name__ == "__main__":