欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2019年10月21日 10:30:23   作者:以夢為馬越騎越傻  
這篇文章主要介紹了python多進程(加入進程池)操作,結(jié)合常見案例形式分析了Python多進程復(fù)制文件、加入進程池及多進程聊天等相關(guān)操作技巧,需要的朋友可以參考下

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

一、多進程復(fù)制多個文件

import multiprocessing
import os
import time
# 復(fù)制文件,傳入文件名
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('執(zhí)行時間:%f' % (time_new - time_old))

二、優(yōu)化加入進程池,并顯示復(fù)制進度:

import multiprocessing
import os
import time
# 復(fù)制文件,傳入文件名
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() #創(chuàng)建隊列對象,用于計算復(fù)制完成百分比
  po = multiprocessing.Pool(3) #創(chuàng)建線程池
  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('執(zhí)行時間:%f' % (time_new - time_old))

三、多進程聊天器:

import multiprocessing
import socket
import threading
# 需求:
# 1.主進程創(chuàng)建一個TCPconnect
# 2.主進程connect后創(chuàng)建進程開啟一個新的Socketconnect
# 3.進程里創(chuàng)建線程不斷的接收和提示發(fā)送消息
# 有連接時新創(chuàng)建一個進程處理聊天
def speak_send(tcp_msg):
  while True:
    test = input('請輸入要發(fā)送的消息')
    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.開線程循環(huán)接收消息
  msg_rec = threading.Thread(target=speak_rec, args=(tcp_msg,))
  # print(tcp_msg.recv(1024).decode('gbk'))
  # 6.開線程循環(huán)發(fā)送消息
  msg_send = threading.Thread(target=speak_send, args=(tcp_msg,))
  msg_rec.start()
  msg_send.start()
  msg_rec.join()
  msg_send.join()
  # 7.關(guān)閉
  # tcp_msg.close()
def main():
  # 1創(chuàng)建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()

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫程序設(shè)計入門教程》及《Python常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • 解決python pandas讀取excel中多個不同sheet表格存在的問題

    解決python pandas讀取excel中多個不同sheet表格存在的問題

    這篇文章主要介紹了解決python pandas讀取excel中多個不同sheet表格存在的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Python 京東云無線寶消息推送功能

    Python 京東云無線寶消息推送功能

    這篇文章主要介紹了Python 京東云無線寶消息推送功能,發(fā)送釘釘消息獲取可用積分,詳細(xì)配置文件通過實例代碼給大家講解的很詳細(xì),代碼+注釋講解的很詳細(xì),需要的朋友可以參考下
    2021-05-05
  • Windows下anaconda安裝第三方包的方法小結(jié)(tensorflow、gensim為例)

    Windows下anaconda安裝第三方包的方法小結(jié)(tensorflow、gensim為例)

    conda的設(shè)計理念——conda將幾乎所有的工具、第三方包都當(dāng)做package對待,甚至包括python和conda自身!因此,conda打破了包管理與環(huán)境管理的約束,能非常方便地安裝各種版本python、各種package并方便地切換
    2018-04-04
  • python pands實現(xiàn)execl轉(zhuǎn)csv 并修改csv指定列的方法

    python pands實現(xiàn)execl轉(zhuǎn)csv 并修改csv指定列的方法

    今天小編就為大家分享一篇python pands實現(xiàn)execl轉(zhuǎn)csv 并修改csv指定列的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python驗證多組數(shù)據(jù)之間有無顯著差異

    python驗證多組數(shù)據(jù)之間有無顯著差異

    這篇文章主要介紹了python驗證多組數(shù)據(jù)之間有無顯著差異,利用方差分析和卡方分布驗證多組數(shù)據(jù)之間的某些屬性有無顯著性差異,對于連續(xù)性屬性可以用方差分析,對于離散型屬性可以用卡方檢驗。下面文章詳細(xì)內(nèi)容需要的小伙伴可以參考一下
    2022-01-01
  • 解決python3 json數(shù)據(jù)包含中文的讀寫問題

    解決python3 json數(shù)據(jù)包含中文的讀寫問題

    這篇文章主要介紹了解決python3 json數(shù)據(jù)包含中文的讀寫問題,需要的朋友可以參考下
    2021-05-05
  • python 進程池的兩種不同實現(xiàn)方法示例

    python 進程池的兩種不同實現(xiàn)方法示例

    這篇文章主要為大家介紹了python 進程池的兩種不同實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • 設(shè)置jupyter中DataFrame的顯示限制方式

    設(shè)置jupyter中DataFrame的顯示限制方式

    這篇文章主要介紹了設(shè)置jupyter中DataFrame的顯示限制方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來
    2020-04-04
  • python爬蟲中采集中遇到的問題整理

    python爬蟲中采集中遇到的問題整理

    在本篇文章里小編給大家整理了關(guān)于python爬蟲中采集中遇到的問題整理內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。
    2020-11-11
  • Python騷操作之動態(tài)定義函數(shù)

    Python騷操作之動態(tài)定義函數(shù)

    這篇文章主要介紹了Python騷操作之動態(tài)定義函數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評論