Python Intefere targets if running too fast

I have a number of threads that are designed to be executed in parallel, as threads are often designed to be executed: D

If I don't slow them down, they tend to puppet towards each other, gaining space for each other. So when I type the expression, I see what looks like "echo" in some streams (not an encoding term, but a literal echo).

When I slow them down, no puppet show happens.

Here's an example of what I get on the terminal when I run these bogus accounts:

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 ten characters.

I really wish I had to post my code, because most Python programmers will probably find out what I don't see in this threading issue, without my 300 line classes being hosted here.

The classes are pretty big, so I won't be posting them here. I suspect this is a common problem that I just don't recognize, like a CPU issue?

What is the common cause of this and how can you get around it without using time delays?

+3


source to share


2 answers


Are these streams accessible to shared data - that is, to the same variable or variables? If so, and if the data is not locked / synchronized, then it is prone to thread race conditions . This is true in any parallel programming environment, not just Python. Check out this good overview of Python sync .



+3


source


This is the standard thread behavior. Do not use streams that share output. Python threads are atomic, which means that if you give them an isolated environment, they won't be a problem for you. If you have access to dynamically modify shared data or write to a shared object or buffer, then you have problems like this.



The problem is the misuse of the stream. Write to a separate buffer inside each thread, and when the threads are done, combine the results.

+1


source







All Articles