1. 程式人生 > 程式設計 >python多程序(加入程序池)操作常見案例

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

本文例項講述了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,'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,))
  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,ip))
    process.start()
if __name__ == '__main__':
  main()

更多關於Python相關內容感興趣的讀者可檢視本站專題:《Python程序與執行緒操作技巧總結》、《Python資料結構與演算法教程》、《Python函式使用技巧總結》、《Python字串操作技巧彙總》、《Python入門與進階經典教程》、《Python+MySQL資料庫程式設計入門教程》及《Python常見資料庫操作技巧彙總》

希望本文所述對大家Python程式設計有所幫助。