Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all - the use of signals should be avoided at all cost:</p> <p>1) It may lead to a deadlock. SIGALRM may reach the process BEFORE the blocking syscall (imagine super-high load in the system!) and the syscall will not be interrupted. Deadlock.</p> <p>2) Playing with signals may have some nasty non-local consequences. For example, syscalls in other threads may be interrupted which usually is not what you want. Normally syscalls are restarted when (not a deadly) signal is received. When you set up a signal handler it automatically turns off this behavior for the whole process, or thread group so to say. Check 'man siginterrupt' on that.</p> <p>Believe me - I met two problems before and they are not fun at all.</p> <p>In some cases the blocking can be avoided explicitely - I strongly recommend using select() and friends (check select module in Python) to handle blocking writes and reads. This will not solve blocking open() call, though.</p> <p>For that I've tested this solution and it works well for named pipes. It opens in a non-blocking way, then turns it off and uses select() call to eventually timeout if nothing is available.</p> <pre><code>import sys, os, select, fcntl f = os.open(sys.argv[1], os.O_RDONLY | os.O_NONBLOCK) flags = fcntl.fcntl(f, fcntl.F_GETFL, 0) fcntl.fcntl(f, fcntl.F_SETFL, flags &amp; ~os.O_NONBLOCK) r, w, e = select.select([f], [], [], 2.0) if r == [f]: print 'ready' print os.read(f, 100) else: print 'unready' os.close(f) </code></pre> <p>Test this with:</p> <pre><code>mkfifo /tmp/fifo python &lt;code_above.py&gt; /tmp/fifo (1st terminal) echo abcd &gt; /tmp/fifo (2nd terminal) </code></pre> <p>With some additional effort select() call can be used as a main loop of the whole program, aggregating all events - you can use libev or libevent, or some Python wrappers around them.</p> <p>When you can't explicitely force non-blocking behavior, say you just use an external library, then it's going to be much harder. Threads may do, but obviously it is not a state-of-the-art solution, usually being just wrong.</p> <p>I'm afraid that in general you can't solve this in a robust way - it really depends on WHAT you block.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload