Thread dummy

Glyph Lefkowitz glyph at twistedmatrix.com
Fri May 12 05:09:11 EDT 2000


Paul Winkler <slinkp23 at yahoo.com> writes:

> Hi,
> 
> Sorry if this sounds dumb...
> 
> Make a thread, let's call it Waiter, that just runs all the time
> and, whenever it receives a message, prints the message to a file.
> 
> Make another thread, let's call it Messenger, that sends a message
> to Waiter at certain times (e.g. in response to a mouse click in a
> GUI).

###
Python 1.5.2 (#0, Apr  3 2000, 14:46:48)  [GCC 2.95.2 20000313 (Debian GNU/Linux)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from threading import *
>>> from Queue import *
>>> q=Queue(100)
>>> def Waiter():
...     while 1:
...             x=q.get()
...             print 'Waiter got a message ->',x 
... 
>>> def Messenger():
...     while 1:
...             x=raw_input("Messenger: ")
...             q.put(x)
... 
>>> w=Thread(target=Waiter)
>>> w.start()
>>> Messenger()
Messenger: hi de ho
Messenger: Waiter got a message -> hi de ho
###

The way that second line of output appears is a bit of foreshadowing
of the nasty tricky things that await you down the path of
multi-threading.

Multithreading could be easy to explain with Python, but I don't know
of any good tutorials for it.  Anyone?

how-many-times-do-i-have-to-do-this-before-i-become-a-'bot'-ly yr's,

-- 
                  __________________________________________
                 |    ______      __   __  _____  _     _   |
                 |   |  ____ |      \_/   |_____] |_____|   |
                 |   |_____| |_____  |    |       |     |   |
                 |   @ t w i s t e d m a t r i x  . c o m   |
                 |   http://www.twistedmatrix.com/~glyph/   |
                 `__________________________________________'




More information about the Python-list mailing list