[Tutor] new to threading, Queues

Jordan wolfrage8765 at gmail.com
Thu Apr 18 11:28:42 CEST 2013


On 04/18/2013 02:16 AM, Chuck Mayers wrote:
> 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!"
True it canbe difficult, but how are you supposed to learn if you don't 
try it, and sometimes you need to do two things at once or at least nearly.
>
> 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
>
Yeah, I am not sure that will work but maybe.  What I did was to model 
the Mediator Pattern kind of, it then controls the queue and assigns the 
workload to multiple threads creating new threads as needed.  Not that 
my way is the best just how I did it.
http://sourcemaking.com/design_patterns/mediator
My Mediator does not follow the design pattern very well, it is called 
dispatcher, and it uses workers to get the Jobs done, jobs are actually 
defined too. Additionally the code is not as good as it could be, 
because I have not used in for many months, but the tests should work.  
It was made with Python3 in mind. It is attached.

> 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!
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Workers.py
Type: text/x-python
Size: 6523 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20130418/44051213/attachment-0001.py>


More information about the Tutor mailing list