如何使另一个python文件中的python函数作为独立进程运行



所以我想做的是,假设有两个python文件A.py和B.py.

A.py


import B
some_code
.
.
.

function_of_B.py()
rest_of_the_code

我遇到的问题是rest_of_the_code等待function_of_B.py()完成执行,我希望function_of_B.py()在并行单元中或独立于A.py执行,这样rest_of_the_code就可以在不等待function_of_B.py()完成的情况下运行。

使用多线程:

import B
import threading
some_code
threading.Thread(target=B.function_of_B).start()
rest_of_the_code

你可以使用:

print("this thread executed by",threading.current_thread().getName())

获取B.py和A.py 中的当前线程名称

最新更新