如何使用python线程池执行none静态函数
How to use python threadpool to execute none static function
我是多线程方面的新手。我在网上浏览了一些文件。我注意到示例使用静态函数作为线程池输入。例如,
def task(n):
time.sleep(3)
print("Processing {}".format(n))
def main():
print("Starting ThreadPoolExecutor")
with ThreadPoolExecutor(max_workers=3) as executor:
future = executor.submit(task,(2))
future = executor.submit(task,(3))
future = executor.submit(task,(4))
future = executor.submit(task,(5))
future = executor.submit(task,(6))
future = executor.submit(task,(7))
future = executor.submit(task,(8))
future = executor.submit(task,(9))
future = executor.submit(task,(10))
以上示例 运行很好。任务并行执行
但是,如果我使用这样的实例中的函数
class Test():
def __init__(self, nums):
self.nums = nums
def test(self):
print("Processing {}".format(str(self.nums)))
time.sleep(3)
def main():
future = executor.submit(Test(2).test())
future = executor.submit(Test(3).test())
future = executor.submit(Test(4).test())
future = executor.submit(Test(5).test())
future = executor.submit(Test(6).test())
future = executor.submit(Test(7).test())
future = executor.submit(Test(8).test())
future = executor.submit(Test(9).test())
future = executor.submit(Test(10).test())
顺序执行,先执行sleep 3秒,再执行第二个。它不再 运行 并行。
我已经尝试过 ThreadPoolExecutor、ProcessPoolExecutor、Pool,它们都执行相同的操作,来自 class 实例的函数无法并行执行。
在Java中,我们可以用Threadpool执行一个Runnable,像这样
Runnable worker = new WorkerThread("" + i);
executor.execute(worker);
Python是否有类似的API表现相同?或者使用 on-static 函数?
比较你的两个例子:
future = executor.submit(task,(2))
future = executor.submit(Test(2).test())
第一个是传递函数和参数。
第二个是调用方法并传递结果。
你要做的就是传递方法:
future = executor.submit(Test(2).test)
我是多线程方面的新手。我在网上浏览了一些文件。我注意到示例使用静态函数作为线程池输入。例如,
def task(n):
time.sleep(3)
print("Processing {}".format(n))
def main():
print("Starting ThreadPoolExecutor")
with ThreadPoolExecutor(max_workers=3) as executor:
future = executor.submit(task,(2))
future = executor.submit(task,(3))
future = executor.submit(task,(4))
future = executor.submit(task,(5))
future = executor.submit(task,(6))
future = executor.submit(task,(7))
future = executor.submit(task,(8))
future = executor.submit(task,(9))
future = executor.submit(task,(10))
以上示例 运行很好。任务并行执行
但是,如果我使用这样的实例中的函数
class Test():
def __init__(self, nums):
self.nums = nums
def test(self):
print("Processing {}".format(str(self.nums)))
time.sleep(3)
def main():
future = executor.submit(Test(2).test())
future = executor.submit(Test(3).test())
future = executor.submit(Test(4).test())
future = executor.submit(Test(5).test())
future = executor.submit(Test(6).test())
future = executor.submit(Test(7).test())
future = executor.submit(Test(8).test())
future = executor.submit(Test(9).test())
future = executor.submit(Test(10).test())
顺序执行,先执行sleep 3秒,再执行第二个。它不再 运行 并行。
我已经尝试过 ThreadPoolExecutor、ProcessPoolExecutor、Pool,它们都执行相同的操作,来自 class 实例的函数无法并行执行。
在Java中,我们可以用Threadpool执行一个Runnable,像这样
Runnable worker = new WorkerThread("" + i);
executor.execute(worker);
Python是否有类似的API表现相同?或者使用 on-static 函数?
比较你的两个例子:
future = executor.submit(task,(2))
future = executor.submit(Test(2).test())
第一个是传递函数和参数。
第二个是调用方法并传递结果。
你要做的就是传递方法:
future = executor.submit(Test(2).test)