I’m actually working with a multi threaded program which involves lots of mysql operations, and basically it’s quite a pain as you have to come up with a smart way to make all queries work. This got me thinking that how you can make a module thread safe.
Anyway I’m trying to ask my question in this way: say you need to constantly append new content to a txt file with lots of different threads, main.py would definitely work as below:
import threading
lock = threading.RLock()
def AppendStr(the_str):
write_thread = threading.Thread(target = RealAppending, args = (the_str, ))
write_thread.start()
def RealAppending(the_str):
lock.acquire()
the_file = open("test.txt", "a")
the_file.append(the_str)
the_file.close()
lock.release()
def WorkerThread(some_arg):
do stuff
AppendStr("whatever you like")
for counter in range(100):
new_thread = threading.Thread(target = WorkerThread, args = (some_arg, ))
new_thread.start()
Well, the thing is, if I’m trying to make the codes neat and easier to maintain, does it still work if I put the codes below into write.py:
import threading
lock = threading.RLock()
def AppendStr(the_str):
write_thread = threading.Thread(target = RealAppending, args = (the_str, ))
write_thread.start()
def RealAppending(the_str):
lock.acquire()
the_file = open("test.txt", "a")
the_file.append(the_str)
the_file.close()
lock.release()
and do it like this in main.py: (I don’t truly understand how import works in python)
import write
def WorkerThread(some_arg):
do stuff
write.AppendStr("whatever you like")
for counter in range(100):
new_thread = threading.Thread(target = WorkerThread, args = (some_arg, ))
new_thread.start()
And also what if there are lots of other modules using write.py in a multi-threaded way, and then you import those modules in main.py and call different def from there. Would everything work out as expected? If not, what should I do to design a ultimate thread-safe module which can be used like this?
If you write.py is imported in lots of other modules, do they all share the same lock? What’s the scope of variables in such modules?
this looks like a threadsave module but there are mistakes:
this is a better version:
because:
the syntax above is for python 2.3 and lower
here is the improved version that does exactly the same:
So yes, your module is threadsave.
There is some stuff that can be added to prevent users from using it in an nonthreadsave manner:
But still you cannot know in which order the strings will be written to the file.