I've seen a similar post here however it refers to Python 2.6 and I was hoping there was an easier way.
From reading the thread it seems the best way is to just replace all my print statements with sys.stdout.write(s + '\n') ?
I was hoping there was a nicer way that allowed me still to use print
The print() function is a built-in function for printing a string on stdout and is not thread-safe.
If a class or a program has immutable state then the class is necessarily thread-safe. Similarly, the shared state in an application where the same thread mutates the state using an operation that translates into an atomic bytecode instruction can be safely read by multiple reader threads.
Yes, they are immutable, just like strings. The code x += 1 actually creates a brand new integer object and assigns it to x . In case it's not clear, things that are immutable are automatically thread safe because there is no way for two threads to try to modify the same thing at the same time.
from __future__ import print_function
print = lambda x: sys.stdout.write("%s\n" % x)
Is a nice cheap and dirty hack.
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