Advertisement
BERKYT

custom_process

Oct 25th, 2023 (edited)
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. from multiprocessing import Process, Queue
  2.  
  3.  
  4. def some_func(a, b, c):
  5.     return a + b + c
  6.  
  7.  
  8. class Worker(Process):
  9.     def __init__(self, func: callable, func_args: list, queue: Queue):
  10.         super().__init__(target=func, args=func_args)
  11.         self.__func = func
  12.         self.__func_args = func_args
  13.         self.__queue = queue
  14.  
  15.     def run(self):
  16.         super().run()
  17.  
  18.         if not q.full():
  19.             result = self.__func(*self.__func_args)
  20.             self.__queue.put(result)
  21.         else:
  22.             print('Очередь полна!')
  23.  
  24.     def start(self):
  25.         return self.run()
  26.  
  27.     def is_alive(self):
  28.         return super().is_alive()
  29.  
  30.     def join(self, **kwargs):
  31.         if self.is_alive():
  32.             return super().join(**kwargs)
  33.  
  34.     def terminate(self):
  35.         if self.is_alive():
  36.             return super().terminate()
  37.  
  38.     def close(self):
  39.         return super().close()
  40.  
  41.  
  42. if __name__ == '__main__':
  43.     q = Queue(2)
  44.  
  45.     w = Worker(some_func, [1, 1, 1], q)
  46.     w.start()
  47.  
  48.     w1 = Worker(some_func, [1, 1, 1], q)
  49.     w1.start()
  50.  
  51.     w2 = Worker(some_func, [1, 1, 1], q)
  52.     w2.start()
  53.  
  54.     if q.full():
  55.         print("Очередь заполнена.")
  56.     else:
  57.         print("Очередь не заполнена.")
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement