def test2(): log = logging.getLogger('') tlog.info('main pid: %d' % os.getpid()) queue = Queue() manager = TaskManager() # task_1 = Task(task1, 80, queue) # task_2 = Task(task2, 80, queue) # manager.add_task(task_1) # manager.add_task(task_2) # manager.add_task(Task(task3, 30)) manager.add_task(Task(task4, 30)) manager.add_task(Task(serial_task, 30)) manager.start()
def task2(queue: Queue): tlog.info("enter task2") tlog.info('wait for queue msg ...') msg = queue.get() tlog.info('get msg: %s' % msg) tlog.info("leave task2") return 0
def test1(): datas = [{'a': d} for d in range(5)] option = parse_args() print(option) time_start = time.time() with Manager() as mng: share_datas = mng.list() with Pool(2) as p: tasks_args = [(data, share_datas) for data in datas] p.map(task, tasks_args) # for data in datas: # tlog.info(data['a']) time_end = time.time() tlog.info('[%d]time_start: %f' % (os.getpid(), time_start)) tlog.info('[%d]time_end: %f' % (os.getpid(), time_end)) tlog.info('[%d]time_diff: %f' % (os.getpid(), time_end - time_start)) tlog.info('len: %d' % len(share_datas))
def task(args): # 参数解包 data, share_datas = args time_start = time.time() time.sleep(0.5) data['a'] = random.randint(0, 10000) share_datas.append(data) time.sleep(0.5) time_end = time.time() tlog.info('[%d]time_diff: %f' % (os.getpid(), time_end - time_start))
def task1(queue: Queue): tlog.info("enter task1") time.sleep(4) msg = 'please exit' tlog.info('put msg: %s' % msg) queue.put(msg) tlog.info("leave task1") return 0
def signal_handler(sig, frame): pid = os.getpid() if manager: manager.stop() tlog.info('pid: %s Canceled all task!' % pid) sys.exit(0)
def on_activate_ctrl_shift_a(): tlog.info('<ctrl>+<shift>+a pressed')
def on_activate_ctrl_c(): tlog.info('<ctrl>+c pressed')
def task5(): for i in range(5): tlog.info("leave task4") time.sleep(0.1) return 0
def task4(): tlog.info("enter task4") time.sleep(5) tlog.info("leave task4") return 0
def task3(): tlog.info("enter task3") time.sleep(10) tlog.info("leave task3") return 1
def on_activate_ctrl_shift_a(): tlog.info('<ctrl>+<shift>+a pressed') def signal_handler(sig, frame): pid = os.getpid() if manager: manager.stop() tlog.info('pid: %s Canceled all task!' % pid) sys.exit(0) signal.signal(signal.SIGINT, signal_handler) if __name__ == "__main__": tlog.info("i'm fine") set_start_method('spawn') # test1() # listener = keyboard.Listener(on_press=on_press, on_release=on_release) # listener.start() # start to listen on a separate thread # listener.join() # remove if main thread is polling self.keys # listener = keyboard.GlobalHotKeys({ # '<ctrl>+c': on_activate_ctrl_c, # '<ctrl>+<shift>+a': on_activate_ctrl_shift_a, # }) # listener.start() test2()