Multi-Threading and KeyboardInterrupt

Mike Kazantsev mk.fraggod at
Tue Jun 16 07:12:17 CEST 2009

On Mon, 15 Jun 2009 15:43:13 -0400
Matt <hellzfury at> wrote:

> I'm going to use the multipocessing library from here forward so I can  
> take advantage of multiple cores and clusters. Either one should work  
> for my use, since in my non-demonstration code each thread spends most  
> of it's time waiting for a separate non-Python subprocess (created  
> with subprocess.Popen) to finish anyway. (I guess Python would see  
> this as IO-blocking) Therefore, if you can fix my toy example with  
> threading, that's fine.
>, followed by a KeyboardInterrupt yields the output in a.out. I  
> want roughly the output in desired.out.
> What do I need to do to modify this code to get my desired output and  
> corresponding functionality? It would be a shame if this wasn't  
> possible in any pure-Python way.

I don't know how complex task you have, but solving trivial IO blocks
with threads or subprocesses look either like ugly hack or an overkill
to me.

Why not just use I/O without blocking?
It's not 80s or 90s anymore, where you had to create subprocess to
handle every non-synchronous task, and since the main burden will be
pushed into non-py subprocesses already, why not implement controller
as a nice, clean and simple single-threaded event loop?

Consider this recipe:

And if the task before you is complex indeed, involving more than just
two to five child processes with a simple "while True: ..." loop,
consider using twisted framework - it'll allow you to do incredible
stuff with any number of sockets with just few lines of code in a
clean, abstracted way.
Latter would also mean that you can always replace os pipes with network
sockets just by changing transport name, distributing your app to any
number of machines.

Mike Kazantsev //
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 205 bytes
Desc: not available
URL: <>

More information about the Python-list mailing list