Current code is:
def export_data(file):
<runs the db2 database command to export tables to file>
def export_to_files(yaml):
logger = logging.getLogger("export_to_files")
thread1 = threading.Thread(target=export_data, args=[out_file1])
thread1.start()
thread2 = threading.Thread(target=export_data, args=[out_file2])
thread2.start()
thread1.join()
thread2.join()
def main():
export_to_files()
if __name__ == "__main__":
main()
My understanding was that join()
only blocks the calling thread. However, I did not realize that thread1.join()
would even block thread2
from executing, essentially making the code to only run 1 thread i.e. thread1
.
How can I execute both the threads concurrently, while have the main thread wait for both to complete?
EDIT: I stand corrected, the 2 threads do run, but it seems like only 1 thread is actually "doing" things at a point in time.
To elaborate further, the callable_method
is reading data from the database and writing to a file. While I can now see 2 files being updated(each thread writes to a separate file), one of the files is not updated for quite some time now, while the other file is up-to-date as to current time.
There is no connection object being used. The queries are run from the db2 command line interface.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…