sleep?

Jason Orendorff jason at jorendorff.com
Thu Jan 24 22:56:22 EST 2002


Peter Hansen wrote:
> maximilianscherr wrote:
> > forgot:
> > i would like to just use the thread module.
> 
> You don't want to use thread.  You might want to use threading.

Why does everyone keep knocking thread?

  >>> import thread, time
  >>> def f():
  ...     time.sleep(5)
  ...     print 'function ran'
  ...
  >>> thread.start_new_thread(f, ())

How can you not love that?  Brief, simple, and clear.


Peter Hansen wrote:
> >>> def f():
> ...   print 'function ran'
> ...
> >>> import threading
> >>> class Delay(threading.Thread):
> ...    def __init__(self, delay, func):
> ...      threading.Thread.__init__(self)
> ...      self.delay = delay
> ...      self.func = func
> ...      if self.func:
> ...         self.start()
> ...    def run(self):
> ...      import time
> ...      time.sleep(self.delay)
> ...      self.func()
> ...
> >>> d = Delay(5, f)  # this begins the delay immediately

Bluh.

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list