import multiprocessing defsender(conn, msgs): """ function to send messages to other end of pipe """ for msg in msgs: conn.send(msg) print("Sent the message: {}".format(msg)) # 不发送了就关闭 conn.close()
defreceiver(conn): """ function to print the messages received from other end of pipe """ whileTrue: msg = conn.recv() if msg == "END": break print("Received the message: {}".format(msg))
if __name__ == "__main__": # messages to be sent msgs = ["hello", "hey", "hru?", "END"] # creating a pipe parent_conn, child_conn = multiprocessing.Pipe() print(parent_conn)
for i inrange(3): t = threading.Thread(target=loop, name='Thread-{}'.format(i)) t.start() # 阻塞调用线程直至线程的join()方法结束 t.join()
print('Main Thread can run now')
# thread Thread-0 is running... # thread Thread-0 ended. # thread Thread-1 is running... # thread Thread-1 ended. # thread Thread-2 is running... # thread Thread-2 ended. # Main Thread can run now
from concurrent.futures import ThreadPoolExecutor import time, threading
pool = ThreadPoolExecutor(5)
deftask(n): print(threading.current_thread().name, 'has task {}'.format(n)) time.sleep(2) return n ** 2
# 回调函数:异步提交之后一旦任务有返回结果,自动交给另外一个去执行 defcall_back(n): print(threading.current_thread().name, "get result {}".format(n.result()))
if __name__ == '__main__': t_list = [] for i inrange(6): # 异步提交任务,通过回掉函数拿到执行结果 future = pool.submit(task, i).add_done_callback(call_back) t_list.append(future) # 异步提交,所以没有返回结果,t_list 全是 None print(t_list) print('Main thread can run others')
# ThreadPoolExecutor-0_0 has task 0 # ThreadPoolExecutor-0_1 has task 1 # ThreadPoolExecutor-0_2 has task 2 # ThreadPoolExecutor-0_3 has task 3 # ThreadPoolExecutor-0_4 has task 4 # [None, None, None, None, None, None] # Main thread can run others
# ThreadPoolExecutor-0_2 get result 4 # ThreadPoolExecutor-0_0 get result 0 # ThreadPoolExecutor-0_2 has task 5 # ThreadPoolExecutor-0_4 get result 16 # ThreadPoolExecutor-0_1 get result 1 # ThreadPoolExecutor-0_3 get result 9 # ThreadPoolExecutor-0_2 get result 25
# 同步非阻塞 from concurrent.futures import ThreadPoolExecutor import time, threading
pool = ThreadPoolExecutor(5)
deftask(n): print(threading.current_thread().name, 'has task {}'.format(n)) time.sleep(2) return n ** 2
if __name__ == '__main__': t_list = [] for i inrange(6): # 同步:提交任务之后,原地等待任务的返回结果,再继续执行下一步代码 # 所以不需要回调 future = pool.submit(task, i) t_list.append(future.result()) # 同步提交能等待结果,所以 list 里面就是结果 print(t_list) print('Main thread can run others')
# ThreadPoolExecutor-0_0 has task 0 # ThreadPoolExecutor-0_0 has task 1 # ThreadPoolExecutor-0_0 has task 2 # ThreadPoolExecutor-0_0 has task 3 # ThreadPoolExecutor-0_0 has task 4 # ThreadPoolExecutor-0_0 has task 5 # [0, 1, 4, 9, 16, 25] # Main thread can run others
deftask(): for i inrange(3): t = threading.Thread(target=loop, name='Thread-{}'.format(i)) t.start() t.join() print("call back here")
t = threading.Timer(2, task) t.start()
print("Main thread can run others")
# Main thread can run others # Thread-0 is running # Thread-0 is ended # Thread-1 is running # Thread-1 is ended # Thread-2 is running # Thread-2 is ended # call back here