我先研究了一下,却找不到答案。我试图在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())
我希望能够几乎同时创建两个目录,因为我每分钟都在统计要创建多少个文件。如果该目录不存在,将会拖延我的时间。