好的,假设我有这个代码:
import time
def helloworld(sleep_time):
while True:
time.sleep(sleep_time)
print('Hello world!')
def hellocountry():
while True:
time.sleep(60)
print('Hello country!')
if __name__ == '__main__':
with Pool(3) as p:
p.map(helloworld, [1, 5, 7])
当 helloworld 正在执行时,我将如何执行 hellocountry?我想我可以编写一个包装函数,但这看起来相当笨拙且不符合Python风格。
最佳答案
只需使用 apply_async
方法。
if __name__ == '__main__':
with Pool(3) as p:
p.apply_async(hellocountry)
p.map(helloworld, [1, 5, 7])
关于python - 如何使用 Python 3 的多处理池运行 2 个不同的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46654626/