当前代码是:
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()
我的理解是join()
只会阻塞调用线程。但是,我没有意识到这thread1.join()
甚至会阻止thread2
执行,基本上使代码只运行 1 个线程,即thread1
.
如何同时执行两个线程,同时让主线程等待两个线程完成?
编辑:我的立场是正确的,2个线程确实运行,但似乎只有1个线程实际上在某个时间点“做”事情。
更详细地说,callable_method
是从数据库中读取数据并写入文件。虽然我现在可以看到 2 个文件正在更新(每个线程写入一个单独的文件),但其中一个文件已经有一段时间没有更新了,而另一个文件是当前时间的最新文件。
没有使用连接对象。查询从 db2 命令行界面运行。