简体   繁体   中英

Managing thread execution in Python

I am currently using worker threads in Python to get tasks from a Queue and execute them, as follows:

from queue import Queue
from threading import Thread

def run_job(item)
    #runs an independent job...
    pass    

def workingThread():
    while True:
        item = q.get()
        run_job(item)
        q.task_done()

q = Queue()
num_worker_threads = 2

for i in range(num_worker_threads):
    t = Thread(target=workingThread)
    t.daemon = True
    t.start()

for item in listOfJobs:
    q.put(item)

q.join()

This is functional, but there is an issue: some of the jobs to be executed under the run_job function are very memory-demanding and can only be run individually. Given that I could identify these during runtime, how could I manage to put the parallel worker threads to halt their execution until said job is taken care of?

Edit: It has been flagged as a possible duplicate of Python - Thread that I can pause and resume , and I have referred to this question before asking, and it surely is a reference that has to be cited. However, I don't think it adresses this situation specifically, as it does not consider the jobs being inside a Queue, nor how to specifically point to the other objects that have to be halted.

I would pause/resume the threads so that they run individually. The following thread Python - Thread that I can pause and resume indicates how to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM