Administrator
2023-08-28 bb868482186f05f70e92dd17e57c80e98bd7d09f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import logging
import multiprocessing
import threading
 
import concurrent.futures
import time
 
from utils import tool
 
 
def task_function(task_param, j):
    # 这里是任务的具体逻辑,task_param是任务的参数
    # 可以在这里进行耗时的计算、IO操作等
 
    print(time.time(), task_param, j, id(threading.current_thread()))
 
    time.sleep(2)
 
    # 返回任务的结果
    return f"result:{task_param}"
 
 
def call_back(result):
    print(time.time(), "call_back", id(threading.current_thread()))
 
 
def run_process_1(pipe):
    thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=20)
 
    def process_data(val):
        print("处理任务", val)
        raise Exception("测试异常")
 
    def recv_data():
        while True:
            try:
                val = pipe.recv()
                if val:
                    thread_pool.submit(process_data, val)
            except Exception as e:
                logging.exception(e)
 
    threading.Thread(target=lambda: recv_data(), daemon=True).start()
    while True:
        time.sleep(2)
 
 
def run_process_2(pipe):
    def send_data():
        while True:
            time.sleep(2)
            for i in range(1):
                pipe.send("test: " + tool.get_now_time_str())
 
    threading.Thread(target=lambda: send_data(), daemon=True).start()
    threading.Thread(target=lambda: send_data(), daemon=True).start()
    while True:
        time.sleep(2)
 
 
if __name__ == '__main__':
    p1, p2 = multiprocessing.Pipe()
    serverProcess = multiprocessing.Process(target=run_process_1, args=(p1,))
    jueJinProcess = multiprocessing.Process(target=run_process_2, args=(p2,))
    serverProcess.start()
    jueJinProcess.start()
 
    while True:
        time.sleep(2)