Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from multiprocessing import Process, Queue
- def some_func(a, b, c):
- return a + b + c
- class Worker(Process):
- def __init__(self, func: callable, func_args: list, queue: Queue):
- super().__init__(target=func, args=func_args)
- self.__func = func
- self.__func_args = func_args
- self.__queue = queue
- def run(self):
- super().run()
- if not q.full():
- result = self.__func(*self.__func_args)
- self.__queue.put(result)
- else:
- print('Очередь полна!')
- def start(self):
- return self.run()
- def is_alive(self):
- return super().is_alive()
- def join(self, **kwargs):
- if self.is_alive():
- return super().join(**kwargs)
- def terminate(self):
- if self.is_alive():
- return super().terminate()
- def close(self):
- return super().close()
- if __name__ == '__main__':
- q = Queue(2)
- w = Worker(some_func, [1, 1, 1], q)
- w.start()
- w1 = Worker(some_func, [1, 1, 1], q)
- w1.start()
- w2 = Worker(some_func, [1, 1, 1], q)
- w2.start()
- if q.full():
- print("Очередь заполнена.")
- else:
- print("Очередь не заполнена.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement