Skip to content

Synchronization issues in threads #5

Description

@luboss

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()
...

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions