0

I am trying to understand Threadpool library. Below is my sample code and it output is as follows:

Code:

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()

Output:

Function 1 - Process ID -  24920 :  ID -  3
Function 1 - Process ID -  24920 :  ID -  2
Function 1 - Process ID -  24920 :  ID -  1

This shows that pool.map creates one system Process with PID = 24920 and executes WorkerProcess function in parallel for 3 myIds: 1,2,3.

Now,

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.

How do I terminate execution peacefully for WorkerProcess function and myId = 1 at 6th second itself rather than continuing its execution? Also, this termination should not harm processing for other ids -WorkerProcess function and myId = 2 and 3

user2961127
  • 963
  • 2
  • 17
  • 29
  • Read [close-a-thread-on-multithreading](https://stackoverflow.com/questions/43683257/python-close-a-thread-on-multithreading) – stovfl Dec 17 '19 at 23:14
  • Does this answer your question? [Timeout function if it takes too long to finish](https://stackoverflow.com/questions/2281850/timeout-function-if-it-takes-too-long-to-finish) – stovfl Dec 17 '19 at 23:16

0 Answers0