我正在尝试在 python 中执行多线程,并且仍在尝试了解可挑选性要求以及我可以保留多少代码 OOP。
你可以从一个类中运行 ThreadPoolExecutor 并传递一个在模块级别定义的函数吗?我的设置如下:
模块A.py:
def foo():
run some stuff
class bar(object):
def runThreads(self):
executor = futures.ThreadPoolExecutor(5)
executor.submit(foo) # do this 5 times in some sort of loop
我希望能够从一个单独的模块中调用它
模块B.py
from moduleA import bar
A = bar()
A.runThreads()
这行得通吗?我还需要导入 foo 吗?