3

I run django with uwsgi server. My code contains many api calls with urllib2 like this:

with closing(urllib2.urlopen(request, timeout=1)) as f:
    content = f.read()

Even when I set socket timeout I see request takes more seconds that I'd like. I guess it happens because timeout limits socket connection but not data reading.

And I want to limit this code block. Tried to achieve it with signal.SIGALRM but it doesn't work with uwsgi (I run without enable-threads). But it works with Apache+mod_wsgi. Thread timeout looks unreliable.

No errors, SIGALRM is just ignored either harakiri mode or not. I'm not good in reading C source code but the reason of such behavior here.

San4ez
  • 8,091
  • 4
  • 41
  • 62
  • I had a very similar question: http://stackoverflow.com/questions/28536469/stopping-a-function-in-python-using-a-timeout – jb. Feb 18 '15 at 17:50

2 Answers2

8

Python compiled with thread support does not call custom signal handlers in its threads or fork()s unless it is reinitialized with PyOS_AfterFork(), which normally happens in os.fork(), but strangely uwsgi does not do this by default. However, it provides an option --py-call-osafterfork, with which uwsgi slaves can handle signals, including SIGALRM.

Orivej Desh
  • 382
  • 4
  • 9
2

do not use UNIX signals for such things, they are basically unreliable. Harakiri mode has a "userlevel" conterpart:

uwsgi.set_user_harakiri(10)
with closing(urllib2.urlopen(request, timeout=1)) as f:
    content = f.read()
uwsgi.set_user_harakiri(0)
roberto
  • 12,723
  • 44
  • 30
  • How can I catch harakiri exception? – San4ez Mar 26 '13 at 08:28
  • you can't as the worker will be reloaded if the function is blocked for more than 10 seconds (or whatever value you set) – roberto Mar 26 '13 at 08:53
  • Actually it's not what I'm looking for =( – San4ez Mar 26 '13 at 09:21
  • then do not use urllib but something like curl that allows you better control over timeouts. I now understand you wanted to use an ALARM to inject an exception in the python VM: this is extremely bad and prone to every kind of error :) – roberto Mar 26 '13 at 09:38