Python - notification of another thread blocked by a subprocess

I am creating a custom web-based task scheduler in python 3.4 on linux. This program creates a daemon (consumer) thread that expects jobs to appear in the PriorityQueue. These tasks can be manually added via the web interface, which adds them to the queue. When the consumer thread finds the job, it runs the program using subprocess.run and waits for it to complete.

The basic idea of ​​a workflow:

class Worker(threading.Thread):

def __init__(self, queue):
    self.queue = queue
    # more code here

def run(self):
    while True:
        try:
            job = self.queue.get()
            #do some work

            proc = subprocess.run("myprogram", timeout=my_timeout)
            #do some more things

        except TimeoutExpired:
            #do some administration
            self.queue.add(job)

But:

  • - ( ), ( ). (, , ), subprocess.run().
  • (, , sme ), , , .
  • .
  • ( , -, ).
  • , (, , ), , . , .

, , ?

+4
1

, : , , . , subprocess.call . , , , - . , subprocess.check_call, , 0. , Linux 0, .

:

class Worker(threading.Thread):

def __init__(self, queue):
    self.queue = queue
    # more code here

def run(self):
    while True:
        try:
            job = self.queue.get()
            #do some work

            subprocess.check_call("myprogram", timeout=my_timeout)
            #do some more things

        except (TimeoutExpired, subprocess.CalledProcessError):
            #do some administration
            self.queue.add(job)

: Python 3.5, subprocess.run check True.

, , , , , , , Python. threading.Event, " " .

, , . , , , , , , Python - . , Global Interpreter Lock ( PDF), , ( ) - CPU.

+1

Source: https://habr.com/ru/post/1613841/


All Articles