[Python-Dev] Non-blocking (asynchronous) timer without thread?

Bob Ippolito bob at redivi.com
Sat Dec 23 08:13:19 CET 2006


On 12/23/06, Evgeniy Khramtsov <xramtsov at gmail.com> wrote:
> Mike Klaas пишет:
>
> > I'm not sure how having python execute code at an arbitrary time would
> > _reduce_ race conditions and/or deadlocks. And if you want to make it
> > safe by executing code that shares no variables or resources, then it
> > is no less safe to use threads, due to the GIL.
> >
> Ok. And what about a huge thread overhead? Just try to start 10-50k
> threading timers :)
>
> > If you can write you application in an event-driven way, Twisted might
> > be able to do what you are looking for.
>
> I don't like an idea of Twisted: you want the banana, but get the whole
> gorilla as well :)

Well you simply can't do what you propose without writing code in the
style of Twisted or with interpreter modifications or evil stack
slicing such as with stackless or greenlet. If you aren't willing to
choose any of those then you'll have to live without that
functionality or use another language (though I can't think of any
usable ones that actually safely do what you're asking). It should be
relatively efficient to do what you want with a thread pool (one
thread that manages all of the timers, and worker threads to execute
the timer callbacks).

FWIW, Erlang doesn't have that functionality. You can wait on messages
with a timeout, but there are no interrupts. You do have cheap and
isolated processes instead of expensive shared state threads, though.
Writing Erlang/OTP code is actually a lot closer to writing Twisted
style code than it is to other styles of concurrency (that you'd find
in Python). It's just that Erlang/OTP has better support for
concurrency oriented programming than Python does (across the board;
syntax, interpreter, convention and libraries).

-bob


More information about the Python-Dev mailing list