Some help needed with small multi-threaded program!

Bryan bryanjugglercryptographer at yahoo.com
Mon May 17 19:29:22 EDT 2010


Oltmans wrote:
> I've a small multi-threaded program that is written by keeping
> Producer-Consumer pattern in mind.
[...]
> Problem is that program sort of runs fine, however, it never
> terminates itself and, more importantly, it doesn't print last-three
> lines of the program(which is important for my purposes). I've looked
> at the docs, but I couldn't figure out a solution so if someone can
> spot the problem and suggest a solution that will be highly
> appreciated. Many thanks in advance.

Well, "the problem" may be a bit presumptuous, but here's *a* problem:

> class TestService(threading.Thread):
[...]
>     def run (self):
>         while True:
>             user = self.queue.get()
>             if user:
>                 self.login(user)

That thread will not exit, and your main thread tries to join it.

In other news. There's no reason to call queue.task_done() if you
never call queue.join(). Your exception handling is kind of whacked;
that errors[] list doesn't collect enough info and if your program
hangs it's no use.

Your Producer-Consumer pattern is a misfit. Registering a user then
trying to log her in is a nice sequential unit of work that one thread
could handle. The reason for threading in this problem is so that when
one user's work is waiting for the network, you can make progress on
other users.

Hope that helps.

-Bryan Olson



More information about the Python-list mailing list