我正在试图理解Threadpool库。下面是我的样本代码,它的输出如下: 代码:
import os
import time
from multiprocessing.dummy import Pool as ThreadPool
class ProcessTest:
def myfunct1(self,id, pid):
print("Function 1 -", "Process ID - ", pid,": ","ID - ", id)
def WorkerProcess(self, myId):
self.myfunct1(myId, os.getpid())
def StartWorkflow(self):
myId = [1,2,3]
pool = ThreadPool(len(myId))
pool.map(self.WorkerProcess, myId)
pool.close()
pool.join()
A = ProcessTest()
A.StartWorkflow()
输出:
Function 1 - Process ID - 24920 : ID - 3
Function 1 - Process ID - 24920 : ID - 2
Function 1 - Process ID - 24920 : ID - 1
这是那个池子。map使用PID = 24920创建一个系统进程,并对3个myid并行执行WorkerProcess函数:1、2、3。 现在,
If WorkerProcess function and myId = 1 it takes 10 seconds to complete.
If WorkerProcess function and myId = 2 it takes 5 seconds to complete.
If WorkerProcess function and myId = 3 it takes 5 seconds to complete.
如何在6秒内终止WorkerProcess函数和myId = 1的执行而不是继续执行呢?此外,此终止不应损害其他id的处理—workerprocess函数和myId = 2和3 问题来源StackOverflow 地址:/questions/59383210/terminate-peacefully-if-execution-times-goes-beyond-certain-threshold-using-pyth
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。