神剑山庄资源网 Design By www.hcban.com
本文实例讲述了python多进程(加入进程池)操作。分享给大家供大家参考,具体如下:
一、多进程复制多个文件
import multiprocessing import os import 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 multiprocessing import os import 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 multiprocessing import socket import 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()
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python进程与线程操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》、《Python+MySQL数据库程序设计入门教程》及《Python常见数据库操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
标签:
python,多进程,加入进程池
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无python多进程(加入进程池)操作常见案例的评论...
更新日志
2024年09月30日
2024年09月30日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】