spawn_later does not work with WSGIServer?

33 views
Skip to first unread message

Jingchao Hu

unread,
Apr 30, 2012, 4:28:05 AM4/30/12
to gevent: coroutine-based Python network library
Hello,

I'm playing with gevent and found a issue with spawn_later and
WSGIServer

The code is as below:

<pre>
from gevent.pywsgi import WSGIServer
from gevent import spawn_later

def application(environ, start_response):
status = '200 OK'
body = 'Hello World!'
headers = [
('Content-Type', 'text/html')
]
start_response(status, headers)
return [body]

def get():
import requests
print requests.get('http://localhost:8000').text

spawn_later(1, get)
spawn_later(2, get)
spawn_later(3, get)

WSGIServer(('', 8000), application).serve_forever()
</pre>

I expected it to print Hello World 3 times after 1/2/3 seconds, but it
just hangs, why? Am I using it in a wrong way?

Denis Bilenko

unread,
Apr 30, 2012, 10:33:48 AM4/30/12
to gev...@googlegroups.com
On Mon, Apr 30, 2012 at 12:28 PM, Jingchao Hu <jingc...@gmail.com> wrote:
> I expected it to print Hello World 3 times after 1/2/3 seconds, but it
> just hangs, why? Am I using it in a wrong way?

Add

from gevent import monkey; monkey.patch_all()

to the beginning of your file.

Without it, request.get() reads a data from a socket in a blocking way
but it since it's blocking, it also blocks the WSGIServer running in
the same process and thus it can never read anything.
Reply all
Reply to author
Forward
0 new messages