国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python多進程(加入進程池)操作常見案例

2019-11-02 14:08:11
字體:
來源:轉載
供稿:網友

本文實例講述了python多進程(加入進程池)操作。分享給大家供大家參考,具體如下:

一、多進程復制多個文件

import multiprocessingimport osimport time# 復制文件,傳入文件名def copy_file(old_file_name, old_name):  new_file_name = 'new_file'  new_name = old_name  if not os.path.exists(new_file_name):    os.makedirs(new_file_name)  with open(old_file_name + '/' + old_name, 'rb') as f:    file_content = f.read()  with open(new_file_name + '/' + new_name, 'wb') as f:    f.write(file_content)if __name__ == '__main__':  old_file_name = 'old_file'  name_list = os.listdir(old_file_name)  time_old = time.time()  for name in name_list:    process = multiprocessing.Process(target=copy_file, args=(old_file_name, name))    process.start()  time_new = time.time()  print('執行時間:%f' % (time_new - time_old))

二、優化加入進程池,并顯示復制進度:

import multiprocessingimport osimport time# 復制文件,傳入文件名def copy_file(old_file_name, old_name, queue):  new_file_name = 'new_file'  new_name = old_name  if not os.path.exists(new_file_name):    os.makedirs(new_file_name)  with open(old_file_name + '/' + old_name, 'rb') as f:    file_content = f.read()  with open(new_file_name + '/' + new_name, 'wb') as f:    f.write(file_content)    queue.put(new_file_name)if __name__ == '__main__':  old_file_name = 'old_file' #存放文件的文件名  name_list = os.listdir(old_file_name) #取出所有文件的文件名  queue = multiprocessing.Manager().Queue() #創建隊列對象,用于計算復制完成百分比  po = multiprocessing.Pool(3) #創建線程池  time_old = time.time() #用于計算花費時間  for name in name_list:    po.apply_async(copy_file, (old_file_name, name, queue))  po.close()  index = 0  while True:    index += 1    queue.get()    print('/r以保存%.2f%%' % ((index / len(name_list)) * 100), end='')    if index == len(name_list):      break  time_new = time.time()  print('執行時間:%f' % (time_new - time_old))

三、多進程聊天器:

import multiprocessingimport socketimport threading# 需求:# 1.主進程創建一個TCPconnect# 2.主進程connect后創建進程開啟一個新的Socketconnect# 3.進程里創建線程不斷的接收和提示發送消息# 有連接時新創建一個進程處理聊天def speak_send(tcp_msg):  while True:    test = input('請輸入要發送的消息')    tcp_msg.send(test.encode('utf-8'))def speak_rec(tcp_msg):  while True:    print(tcp_msg.recv(1024).decode('gbk'))# 開啟的進程聊天def speak_process(tcp_sock, tcp_msg, ip):  print('開啟進程')  # 5.開線程循環接收消息  msg_rec = threading.Thread(target=speak_rec, args=(tcp_msg,))  # print(tcp_msg.recv(1024).decode('gbk'))  # 6.開線程循環發送消息  msg_send = threading.Thread(target=speak_send, args=(tcp_msg,))  msg_rec.start()  msg_send.start()  msg_rec.join()  msg_send.join()  # 7.關閉  # tcp_msg.close()def main():  # 1創建TCP對象  tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 2.綁定ip和端口  tcp_sock.bind(('', 9999))  # 3.改主動為被動  tcp_sock.listen(128)  # 4.accept接收msg和ip  while True:    tcp_msg, ip = tcp_sock.accept()    process = multiprocessing.Process(target=speak_process, args=(tcp_sock, tcp_msg, ip))    process.start()if __name__ == '__main__':  main()
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 改则县| 合肥市| 准格尔旗| 博野县| 汶上县| 阿拉尔市| 沙湾县| 襄城县| 彰化市| 偃师市| 商丘市| 塔城市| 基隆市| 恩施市| 类乌齐县| 那曲县| 习水县| 左云县| 宁明县| 镇雄县| 鹰潭市| 雅安市| 罗城| 封丘县| 邛崃市| 海城市| 平果县| 潼关县| 海兴县| 巩留县| 泸水县| 东方市| 奉新县| 忻州市| 扎鲁特旗| 大渡口区| 井冈山市| 江达县| 达孜县| 澄城县| 资阳市|