多线程:如何运行在不同的线程实现不同的功能?

Multi-threading: How to run different functions in different threads?

如何在 python 的不同线程中 运行 两个或三个不同的函数?

例如,如果我创建了一些示例函数,如下所示。

def method1(x,y):
     calc = x*y
     print(calc)

def method2(x,y):
     calc = x+y
     print(calc)

def method3(x,y):
     calc = x-y
     print(calc)

我如何才能在不同的线程中同时 运行 所有三个函数?

使用 ThreadPoolExecutor 的示例(在 python3 中可用)

from concurrent.futures import ThreadPoolExecutor

def method1(x,y):
     calc = x*y
     print(calc)

def method2(x,y):
     calc = x+y
     print(calc)

def method3(x,y):
     calc = x-y
     print(calc)

pool = ThreadPoolExecutor(max_workers=3)
x = 5
y = 6
pool.submit(method1, x, y)
pool.submit(method2, x, y)
pool.submit(method3, x, y)