python Pool进程池 用Manager().Queue()通信 主进程却无法get到Queue里的数据 不知道为什么? python3 进程池pool使用失败

python3 \u8fdb\u7a0b\u6c60pool=5\uff1a\u6211\u6709100\u4e2a\u4efb\u52a1\u8981\u6267\u884c\uff1f

\u4e0d\u9700\u8981\uff0c\u6267\u884c\u5b8c\u4e86\u4e00\u4e2a\u4efb\u52a1\uff0c\u8fdb\u7a0b\u4f1a\u8fd4\u56de\u6c60\u4e2d\u5f85\u547d

\u7b2c\u4e00\u4e2a\u8fdb\u7a0b\u7684\u65f6\u5019\u4f60\u5efa\u6587\u4ef6\u7684\u65f6\u5019\u7528os.open(\u2018your_lockfile\u2019,os.O_CREAT|os.O_EXCL|os.O_RDWR) \u7b2c\u4e8c\u4e2a\u8fdb\u7a0b\u91cc\u4f60\u5148\u770b\u8fd9\u4e2a\u6587\u4ef6\u6709\u6ca1\u6709\uff0c\u5982\u679c\u6709\u5c31try\u5220\u9664\u5b83\uff0c\u7136\u540eexcept OSError as e\uff0c\u5982\u679ce.errno==13\u5c31\u8bf4\u660e\u6709\u7b2c\u4e00\u4e2a\u8fdb\u7a0b\u5728\u8fd0\u884c\u3002

路径不要自己拼接, 容易出错:

import os
import time
from multiprocessing import Pool, Manager, cpu_count


def copy(file_name, old_folder, new_folder, queue):
    print(os.path.join(old_folder, file_name))
    fr = open(os.path.join(old_folder, file_name), "r")
    fw = open(os.path.join(new_folder, file_name), "w")
    fw.write(fr.read()())
    fr.close()
    fw.close()
    queue.put(file_name)


def main():
    base_path = os.getcwd()
    old_folder = os.path.join(base_path, input("please your should copy the folder name:
"))
    new_folder = os.path.join(base_path, old_folder + "复件")
    files = os.listdir(old_folder)
    os.system("rmdir /Q /S {}".format(new_folder))
    os.mkdir(new_folder)
    pool = Pool(cpu_count())
    queue = Manager().Queue()
    for filename in files:
        pool.apply_async(copy, (filename, old_folder, new_folder, queue))
    pool.close()
    num = 0
    while True:
        time.sleep(1)
        all_file = len(files)
        if queue.empty():
            break
        queue.get()
        num += 1
        print("copy: {0:.2f}%".format(num / all_file * 100), end="")
    pool.join()


if __name__ == "__main__":
    main()


扩展阅读:pubg苹果手机下载入口 ... python mysql库 ... tkinter做的漂亮界面 ... python xml ... python bool ... pagescope mobile5.0 安卓 ... path browser在python ... python serial库 ... pubg mobile ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网