0

在使用模板重命名文件并从 for 循环中附加递增数字后,我有一个函数将(基于参数)将图像文件从 Windows 中的映射读卡器驱动器移动/重命名到服务器驱动器。

通常有三张照片卡将被发送到目标文件夹。目前,每张卡都在一个接一个地处理,由于文件大小和通过网络传输可能需要相当长的时间。

有没有办法让函数接收映射的卡驱动器列表(不超过 3 个),然后为每张卡同时运行重命名功能。

我试图说明我正在尝试做的事情的糟糕尝试如下:

def collectCards(cards):
    for card in cards:
         #my goal would be to run each instance of the following function asynchronously
         drv =renameImages(card)

def renameImages(cardDrive):
    #perform renaming functions
    return count_of_renamed_images
4

1 回答 1

2

您可以尝试将多处理与进程 ( Pool) 或线程 ( pool.ThreadPool) 一起使用。在这两种情况下,唯一的区别在于导入 - API 保持不变:

from multiprocessing import Pool

my_pool = Pool(3)
# `cards` is a list of cards. Send rename requests to workers.
my_pool.map(renameImages, cards)

# Close the pool and wait for processes to close.
my_pool.close()
my_pool.join()

中的数字Pool(3)表示工作进程的数量 - 更多意味着运行的并发 renameImages 函数数量更多。请记住,这multiprocessing要求card对象能够被腌制。如果renameImages内存不重,您可以尝试使用ThreadPool-card然后在线程之间共享对象。

于 2013-08-10T19:33:12.010 回答