You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
from multiprocessing import Queue, Process
|
|
|
|
|
# from time import sleep
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Worker:
|
|
|
|
|
def __init__(self, loop):
|
|
|
|
|
self._output_queue = Queue()
|
|
|
|
|
self._in_queue = Queue()
|
|
|
|
|
self._event_loop = loop
|
|
|
|
|
|
|
|
|
|
async def send(self, data: dict):
|
|
|
|
|
self._in_queue.put(data)
|
|
|
|
|
|
|
|
|
|
async def get(self):
|
|
|
|
|
data = await self._event_loop.run_in_executor(None, self._get_output,
|
|
|
|
|
self._output_queue)
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _get_output(output_queue: Queue) -> dict:
|
|
|
|
|
return output_queue.get()
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _main_loop(command, in_queue: Queue, out_queue: Queue):
|
|
|
|
|
data = in_queue.get()
|
|
|
|
|
print('\nworker for command:', command)
|
|
|
|
|
output = {"type": "log",
|
|
|
|
|
"level": "INFO",
|
|
|
|
|
"msg": f"recieved message {data['text']}"}
|
|
|
|
|
out_queue.put(output)
|
|
|
|
|
|
|
|
|
|
def run(self, command):
|
|
|
|
|
'''Метод для запуска процесса воркера с заданным '''
|
|
|
|
|
worker_process = Process(target=self._main_loop,
|
|
|
|
|
args=(command,
|
|
|
|
|
self._in_queue,
|
|
|
|
|
self._output_queue))
|
|
|
|
|
worker_process.start()
|