我正在研究多处理。我编写了如下代码。
import multiprocessing as mp
def foo(q):
while q.empty() is not True:
filename_with_full_path = q.get()
...processing the file...
return
if __name__ == "__main__":
q = mp.Queue()
p1 = mp.Process(target=foo, args=(q))
p2 = mp.Process(target=foo, args=(q))
p1.start()
p2.start()
file_counter = 0
for filename in os.listdir(directory):
if filename.endswith(".jpg"):
filename_with_full_path = directory + '/' + filename
q.put(filename_with_full_path)
file_counter += 1
while q.empty() is not True:
pass
p1.join()
p2.join()
事情是它的工作!但是,一切都很可疑。我应该在“put / get”方法调用时实际使用锁定吗?