Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83833

Re: How to terminate the function that runs every n seconds

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: How to terminate the function that runs every n seconds
Date 2015-01-15 17:29 +0200
Organization A noiseless patient Spider
Message-ID <878uh4awpk.fsf@elektro.pacujo.net> (permalink)
References (3 earlier) <CACT3xuXSuXyRu8ReXLczFw9N_LEajQzDM=eccdU3GPmj4MgYLA@mail.gmail.com> <mailman.17737.1421283049.18130.python-list@python.org> <87r3uwpetd.fsf@elektro.pacujo.net> <mailman.17761.1421331127.18130.python-list@python.org> <87k30ob052.fsf@elektro.pacujo.net>

Show all headers | View raw


Marko Rauhamaa <marko@pacujo.net>:
> Dennis Lee Bieber <wlfraed@ix.netcom.com>:
>> 	My response to that then is: design the thread's I/O so that it
>> is not blocking... On Linux, maybe a timed select(); Windows? short
>> sleeps around a non-blocking check for available data... (if console
>> I/O, msvcrt.kbhit(); otherwise may need some other library function to
>> put a time-out on the I/O)
>
> Ah, polling, the fig leaf that covers embarrassing design constraints
> all over the world.

And to provide something constructive, I should add:

Polling is occasionally the right way forward (consider the Linux
kernel's NAPI, for example). However, it is usually bad because:

 * it consumes resources when there is no need: the CPU wakes up from
   the power-saving mode, the hard disk spins unnecessarily; the battery
   that should be good for several years, dies in the middle of the
   winter

 * it is not reactive enough as you must wait patiently for the polling
   interval to expire.


Since you brought up select(), a better choice is right under your nose:
multiplexing. Have your program block and be prepared for any possible
stimulus. However, once you do that, you realize you lose the naive
simplicity of threads. Thus we arrive at the conclusion: asynchronous
programming and processes are usually a better design choice than
threads.


Marko

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: How to terminate the function that runs every n seconds Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-01-14 19:35 -0500
  Re: How to terminate the function that runs every n seconds Marko Rauhamaa <marko@pacujo.net> - 2015-01-15 11:34 +0200
    Re: How to terminate the function that runs every n seconds Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-01-15 09:11 -0500
      Re: How to terminate the function that runs every n seconds Marko Rauhamaa <marko@pacujo.net> - 2015-01-15 16:15 +0200
        Re: How to terminate the function that runs every n seconds Marko Rauhamaa <marko@pacujo.net> - 2015-01-15 17:29 +0200

csiph-web