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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With