multi_progress icon indicating copy to clipboard operation
multi_progress copied to clipboard

Synchronization issues in threads

Open luboss opened this issue 6 years ago • 0 comments

First of all thank you for the ideas and code. I was looking for exactly such solution.

However your code has big flaw which might not that obvious and is difficult to encounter on slower machines. The output can get messed when multiple threads are writing to the terminal at same time. What you lack is synchronization between the threads using locking mechanism. Following code change solves this issue:

from multiprocessing import Pool, Lock
...

lock = Lock()

...

    def write(self, string):
        lock.acquire()
        try:
            with term.hidden_cursor():                 # this is optional
                with term.location(*self.location):
                    print(string)
        finally:
            lock.release()
...

luboss avatar Jul 30 '19 11:07 luboss