我正在尝试在 Python 中并行运行多个函数。
我有这样的事情:
files.py
import common #common is a util class that handles all the IO stuff
dir1 = 'C:\folder1'
dir2 = 'C:\folder2'
filename = 'test.txt'
addFiles = [25, 5, 15, 35, 45, 25, 5, 15, 35, 45]
def func1():
c = common.Common()
for i in range(len(addFiles)):
c.createFiles(addFiles[i], filename, dir1)
c.getFiles(dir1)
time.sleep(10)
c.removeFiles(addFiles[i], dir1)
c.getFiles(dir1)
def func2():
c = common.Common()
for i in range(len(addFiles)):
c.createFiles(addFiles[i], filename, dir2)
c.getFiles(dir2)
time.sleep(10)
c.removeFiles(addFiles[i], dir2)
c.getFiles(dir2)
我想调用 func1 和 func2 并让它们同时运行。这些函数不会相互交互或在同一对象上交互。现在我必须等待 func1 完成然后 func2 开始。我该如何做如下所示的事情:
process.py
from files import func1, func2
runBothFunc(func1(), func2())
我希望能够非常接近地同时创建两个目录,因为我每分钟都会计算正在创建的文件数量。如果目录不存在,我的时间就会被打乱。