Python Threads Intefere if Executed Too Quickly

198 views Asked by At

I have a number of threads which are meant to execute in parallel, as threads are often meant to do :D

Unless I slow them down, they tend to puppet one-another, writing to eachother's space. Thus when I print a statement, I see what appears to be an "echo" (not the coding term, but a literal echo) in some threads.

When I do slow them down, no puppetry occurs.

Here is an example of what I get on the terminal when these dummy accounts run:

Meikitotokiku83: day_2, Wed Jun 10 03:32:53 2015, nine
Robinia6424: day_2, Wed Jun 10 03:32:53 2015, four
Meikitotokiku83: day_7, Wed Jun 10 03:32:53 2015, fifteen
Meikitotokiku83: day_7, Wed Jun 10 03:32:53 2015, thirteen
Mekushishifu643: day_7, Wed Jun 10 03:32:53 2015, two
Meikitotokiku83: day_7, Wed Jun 10 03:32:53 2015, eleven
Meikitotokiku83: day_2, Wed Jun 10 03:32:53 2015, six
Mekushishifu643: day_2, Wed Jun 10 03:32:53 2015, three
**Meikitotokiku83: day_7, Wed Jun 10 03:32:53 2015, ten 
Meikitotokiku83: day_7, Wed Jun 10 16:33:03 2015, ten 
 Meikitotokiku83: day_7, Wed Jun 10 16:33:03 2015, ten 
Meikitotokiku83: day_7, Wed Jun 10 16:33:03 2015, ten** 
Meikitotokiku83: day_2, Wed Jun 10 03:32:53 2015, fourteen
Meikitotokiku83: day_7, Wed Jun 10 03:32:53 2015, sixteen
DaDaFurstig6304: day_7, Wed Jun 10 03:32:53 2015, five
DoraDiggle5529: day_7, Wed Jun 10 03:32:53 2015, one
Meikitotokiku83: day_2, Wed Jun 10 03:32:53 2015, seven

Notice the echo that occurs in the ones labeled 'ten'.

I really wish I didn't have to post my code, because most python programmers will probably recognize what I am not in this threading problem without my 300-line classes being posted here.

The classes are fairly large, so I won't bother posting them here. I suspect this is a common problem I am just not recognizing, such as a processor issue?

What is a common cause of this, and how can it be circumvented without using time delays?

2

There are 2 answers

3
sparc_spread On BEST ANSWER

Are these threads accessing shared data - that is, the same variable or variables? If so, and if the data is unprotected with locks/synchronization, then it is susceptible to thread race conditions. This is true in any concurrent programming environment, not just Python. Check out this nice summary of Python synchronization.

2
Alex Huszagh On

This is standard thread behavior. Do not use threads that have a shared output. Python's threads are atomic: that means if you give them an isolated environment, they will not give you problems. If you have them access shared data being dynamically modified, or write to a shared object or buffer, then you will get problems like this.

The problem is you improperly using a thread. Write to a separate buffer inside each thread, and when the threads are done, then combine the results.