开发者社区 问答 正文

如果使用python执行时间超过某个阈值,则和平终止

我正在试图理解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

展开
收起
kun坤 2019-12-27 16:27:04 622 分享 版权
1 条回答
写回答
取消 提交回答
问答分类:
问答地址: