Derek Fountain wrote:[color=blue]
> Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
> this bit of code" sort of thing?[/color]
"Noen" <not.available@ na.no> wrote in message news:zp4nb.2357 7$BD3.4439081@j uliett.dax.net. ..[color=blue]
> Derek Fountain wrote:[color=green]
> > Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
> > this bit of code" sort of thing?[/color]
> http://www.python.org/Doc/lib/module-sched.html
>[/color]
> > > Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run[color=blue][color=green][color=darkred]
> > > this bit of code" sort of thing?[/color][/color][/color]
t = threading.Timer (30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Dave Harrison wrote:
[color=blue][color=green][color=darkred]
>> > > Does Python have a timer mechanism? i.e. an "after 500 milliseconds,
>> > > run this bit of code" sort of thing?[/color][/color]
>
> why not just use the time.sleep() function ?[/color]
Because that stops the thread. I want things to continue, and then be
interrupted in order to execute a bit of code. Tcl has the 'after' command:
after 5000 { set disabled 0 }
The script carries on, and after 5 seconds whatever is being done gets
interrupted in order to run a bit of script - in this case set a variable
turning off a disablement of some sort. This makes it trivial to implement
"disable this feature for 5 seconds" kinds of logic.
Alan Kennedy wrote:
[color=blue]
> [Derek Fountain][color=green]
>> Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
>> this bit of code" sort of thing?[/color]
>
> Like this?
> http://www.python.org/doc/current/li...r-objects.html[/color]
That looks like what I'm after, yes. I haven't looked into threads since
they've represented trouble in every other language I've used. But I'll
have a look into their implementation in Python and see if they're any more
usable here...
At 9:04 PM +0800 27/10/03, Derek Fountain wrote:[color=blue]
>Dave Harrison wrote:
>[color=green][color=darkred]
>>> > > Does Python have a timer mechanism? i.e. an "after 500 milliseconds,
>>> > > run this bit of code" sort of thing?[/color]
>>
>> why not just use the time.sleep() function ?[/color]
>
>Because that stops the thread. I want things to continue, and then be
>interrupted in order to execute a bit of code. Tcl has the 'after' command:
>
>after 5000 { set disabled 0 }
>
>The script carries on, and after 5 seconds whatever is being done gets
>interrupted in order to run a bit of script - in this case set a variable
>turning off a disablement of some sort.[/color]
Wow, that must make your scripts so much more interesting to debug ;)
Anthony
--
----------------------------------------------------
HyPEraCtiVE? HeY, WhO aRE YoU cALliNg HypERaCtIve?! aBRiGgS@wEStNeT .cOm.aU
----------------------------------------------------
At 09:21 PM 10/26/2003, Derek Fountain wrote:
[color=blue]
>Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
>this bit of code" sort of thing?[/color]
In article <3f9d1775$0$235 84$5a62ac22@fre enews.iinet.net .au>,
Derek Fountain <nomail@hursley .ibm.com> wrote:[color=blue]
>Dave Harrison wrote:
>[color=green][color=darkred]
>>> > > Does Python have a timer mechanism? i.e. an "after 500 milliseconds,
>>> > > run this bit of code" sort of thing?[/color]
>>
>> why not just use the time.sleep() function ?[/color]
>
>Because that stops the thread.[/color]
Check out the threading module. In particular, the threading.Timer class.
[Cameron Laird][color=blue]
> This thread is really about asynchronous or concurrent
> processing,[/color]
I really don't have time to post to the level of detail I would like,
but I had to put forward a couple of thoughts.
[color=blue]
> I claim, and I further claim we really don't
> have *any* satisfying model for coding those.[/color]
I think you might get some disagreement from the Twisted, Medusa and
ZServer people. And possibly see a slanging match erupt on whose
design is best....
[color=blue]
> Threading
> is a quagmire (everyone agree?),[/color]
Definitely. Sometimes.
I think threads are really a useful abstraction for programmers who
are learning to deal with developing servers for the first time. And
threads fulfill their function very well, to a point.
But they have fundamental scalability problems: The C10K problem
illustrates this well: Surely it should be possible for a modern
computer, with giga-everything (!), to serve 10,000 clients
simultaneously?
To go beyond the performance limits imposed by the resource-hogging
threads (OMG, > 1MB of core on some platforms!), it's necessary to
move to a different concurrency mechanism, such as coroutines, which
represents "tasklets" much more efficiently.
But event-based concurrency models suffer from a fundamental
limitation: it is more difficult to spread work across multiple
processors. CPython, to some degree, lets event-based developers off
the hook, because the presence of the GIL allows them to not address
the problem, because it wouldn't achieve anything anyway.....
And the patchy support for SSL in asynch frameworks is another
fundamental problem.
Another point in favour of threads: On a multi-processor box, the
thread abstraction generally "automagica lly" gives you free migration
to other processors. You (generally) don't have to change a line of
your code: the underlying [OS|VM] takes care of it.
[color=blue]
> and our guild has demonstrated only marginally more
> reliability in working with,
> say, co-routines, chords, continuations, and so on. For
> my money, the event-oriented model behind the [after]
> above is at least as robust as any other.[/color]
FWIW, I think that Java has a very nice model for asynchronous IO,
with all "selectors" being thread-safe, so that "selectable s" that are
ready for IO can be processed in another thread, i.e. potentially on
another processor, thus giving the best of both worlds.
[color=blue]
> Among other frailties, I hold a grudge against the aca-
> demic world that it hasn't shown more leadership in this
> matter.[/color]
Googling for "'high performance' asynchronous server" shows that both
Academia and Industry are certainly not short of powerpoint-ware on
the subject......
[color=blue]
> I'll summarize: validation of event-oriented designs
> implemented with [after] and similar mechanisms, is at
> worst no more difficult than validation of the corre-
> sponding thread-based designs. Concurrency is hard to
> get right, at least with our current understanding.[/color]
My €0,02: Generators (and, by extension, coroutines) will be python's
greatest advance in simplifying writing event-based server
architectures: resumable functions are a honking great idea.
[Cameron Laird][color=blue]
> This thread is really about asynchronous or concurrent
> processing,[/color]
I really don't have time to post to the level of detail I would like,
but I had to put forward a couple of thoughts.
[color=blue]
> I claim, and I further claim we really don't
> have *any* satisfying model for coding those.[/color]
I think you might get some disagreement from the Twisted, Medusa and
ZServer people. And possibly see a slanging match erupt on whose
design is best....
[color=blue]
> Threading
> is a quagmire (everyone agree?),[/color]
Definitely. Sometimes.
I think threads are really a useful abstraction for programmers who
are learning to deal with developing servers for the first time. And
threads fulfill their function very well, to a point.
But they have fundamental scalability problems: The C10K problem
illustrates this well: Surely it should be possible for a modern
computer, with giga-everything (!), to serve 10,000 clients
simultaneously?
To go beyond the performance limits imposed by the resource-hogging
threads (OMG, > 1MB of core on some platforms!), it's necessary to
move to a different concurrency mechanism, such as coroutines, which
represents "tasklets" much more efficiently.
But event-based concurrency models suffer from a fundamental
limitation: it is more difficult to spread work across multiple
processors. CPython, to some degree, lets event-based developers off
the hook, because the presence of the GIL allows them to not address
the problem, because it wouldn't achieve anything anyway.....
And the patchy support for SSL in asynch frameworks is another
fundamental problem.
Another point in favour of threads: On a multi-processor box, the
thread abstraction generally "automagica lly" gives you free migration
to other processors. You (generally) don't have to change a line of
your code: the underlying [OS|VM] takes care of it.
[color=blue]
> and our guild has demonstrated only marginally more
> reliability in working with,
> say, co-routines, chords, continuations, and so on. For
> my money, the event-oriented model behind the [after]
> above is at least as robust as any other.[/color]
FWIW, I think that Java has a very nice model for asynchronous IO,
with all "selectors" being thread-safe, so that "selectable s" that are
ready for IO can be processed in another thread, i.e. potentially on
another processor, thus giving the best of both worlds.
[color=blue]
> Among other frailties, I hold a grudge against the aca-
> demic world that it hasn't shown more leadership in this
> matter.[/color]
Googling for "'high performance' asynchronous server" shows that both
Academia and Industry are certainly not short of powerpoint-ware on
the subject......
[color=blue]
> I'll summarize: validation of event-oriented designs
> implemented with [after] and similar mechanisms, is at
> worst no more difficult than validation of the corre-
> sponding thread-based designs. Concurrency is hard to
> get right, at least with our current understanding.[/color]
My €0,02: Generators (and, by extension, coroutines) will be python's
greatest advance in simplifying writing event-based server
architectures: resumable functions are a honking great idea.
The official home of the Python Programming Language
It is easy to create your own timer objects by using
python threads.
Here is a sample code for the same. It is a working
code, and creates a custom timer class with a maximum
timeout, which calls the function again and again until
it times out. It is different from the standard timer
which calls its target function just once.
----------------<snip>-----<snip>--------------------
from threading import Thread
import time
Bob Gailer <bgailer@alum.r pi.edu> wrote in message news:<mailman.1 36.1067267376.7 02.python-list@python.org >...[color=blue]
> At 09:21 PM 10/26/2003, Derek Fountain wrote:
>[color=green]
> >Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
> >this bit of code" sort of thing?[/color]
>
> Check out the sched and time modules.
>
> Bob Gailer
> bgailer@alum.rp i.edu
> 303 442 2625
>
> --[/color]
In article <3F9D77BE.B66D1 F08@hotmail.com >,
Alan Kennedy <alanmk@hotmail .com> wrote:[color=blue]
>[Cameron Laird][color=green]
>> This thread is really about asynchronous or concurrent
>> processing,[/color]
>
>I really don't have time to post to the level of detail I would like,
>but I had to put forward a couple of thoughts.[/color]
This is mostly a "me, too" follow-up. I decided
to post it, though, if only to ensure[color=blue]
>[color=green]
>> I claim, and I further claim we really don't
>> have *any* satisfying model for coding those.[/color]
>
>I think you might get some disagreement from the Twisted, Medusa and
>ZServer people. And possibly see a slanging match erupt on whose
>design is best....[/color]
that Twisted et al. don't feel abused. I am VERY
fond of Twisted, Medusa, and Zope, and have even
written an (unpublished, still) article on their
concurrency management. I, also, won't go into
detail now; I just want to make clear that I think
they're great, and was excluding them only for
categorical reasons.
Comment