added definition of external worker process
This commit is contained in:
@@ -0,0 +1,40 @@
|
|||||||
|
from multiprocessing import Process, Queue
|
||||||
|
import time
|
||||||
|
from typing import List
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class Worker(object):
|
||||||
|
def __init__(self, queue: Queue):
|
||||||
|
self.run_flag = True
|
||||||
|
self.queue = queue
|
||||||
|
self.process = Process(
|
||||||
|
target=self.handle_jobs,
|
||||||
|
args=(self.queue,)
|
||||||
|
)
|
||||||
|
time.sleep(1)
|
||||||
|
self.process.start()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.queue.put(['exit'])
|
||||||
|
self.process.join()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def handle_jobs(queue: Queue):
|
||||||
|
while True:
|
||||||
|
if queue.empty():
|
||||||
|
time.sleep(1)
|
||||||
|
continue
|
||||||
|
cmd_list: List[str] = queue.get()
|
||||||
|
print('received job:\n', cmd_list)
|
||||||
|
if cmd_list[0].lower() == 'exit':
|
||||||
|
break
|
||||||
|
cmd_str = ' && '.join(cmd_list)
|
||||||
|
os.system(cmd_str)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
work_queue: Queue = Queue()
|
||||||
|
worker = Worker(queue=work_queue)
|
||||||
|
work_queue.put(['echo "hello world"', 'echo "hello world again"'])
|
||||||
|
worker.stop()
|
||||||
Reference in New Issue
Block a user