[Tutor] new to threading, Queues

Chuck Mayers chuck.mayers at gmail.com
Thu Apr 18 02:16:38 CEST 2013


Hi!  I was having a bit of nostalgia today, and thought I'd try to write a
simple, old school BBS.  I found the 'paramiko' library, and I've got
something I can SSH into that would have impressed my 1990's self.

I found some example code of the "threading" library, and I've managed to
use it to take multiple incoming connections at once.

I've never done any multithreaded programming, and everything I've ever
read is essentially "don't do it! It's really hard to get right!"

Everything I've read today says to use the Queue library
I can't seem to wrap my head around it, though. I've not seen any example
code that looks like what I'm trying to do.

Some examples of things I'd like to do:
Have one thread open a file (that all threads will want to read/write to)
and update it
Have one thread broadcast to all other threads a "chat message"


The only way I can think of, with the Queue library, would be the following
(I know this has to be the "wrong way" to do this):

1. create an object MultiTask which will have methods to handle thread
sensitive things like "update a file", etc
2. create one instance of it (we'll call it mtask) and add it to the queue
3. each thread, when it needs to, does queue.get() to get that instance
4. the thread then calls whatever it needs to, say mtask.update_users_file()
5. then, call queue.put(mtask) to put it back on the queue

so, there is only ever 1 object in the queue. I assume when another thread
tries to get() it, they block until the other threads are done with it.

Will this work? Is there a better way to do this?

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130417/290b4282/attachment.html>


More information about the Tutor mailing list