我有这个脚本,但它返回了一个错误。问题是我遗漏了写
if _name_ == "__main__":
这是我从教程中获得的脚本,但我正在根据我的需要进行调整
import concurrent.futures
import time
start = time.perf_counter()
def do_something(seconds):
print(f'Sleeping {seconds} second(s)...')
time.sleep(seconds)
return f'Done Sleeping...{seconds}'
with concurrent.futures.ProcessPoolExecutor() as executor:
secs = [5, 4, 3, 2, 1]
results = executor.map(do_something, secs)
# for result in results:
# print(result)
finish = time.perf_counter()
print(f'Finished in {round(finish-start, 2)} second(s)')
我遇到的问题是,我使用的是Windows,我不知道该在哪里写:if __name__ == "__main__":
您的代码需要导入安全,因为multiprocessing
将在子流程中重新导入它。这意味着您不应该在模块级别执行任何您不希望仅在导入时运行的操作。例如,在模块级别创建执行器意味着您将创建无限多的子流程,因为每个新的子流程都将导入模块并创建另一个子流程。
以下是如何解决问题
import concurrent.futures
import time
def do_something(seconds):
print(f'Sleeping {seconds} second(s)...')
time.sleep(seconds)
return f'Done Sleeping...{seconds}'
if __name__ == "__main__":
start = time.perf_counter()
with concurrent.futures.ProcessPoolExecutor() as executor:
secs = [5, 4, 3, 2, 1]
results = executor.map(do_something, secs)
# for result in results:
# print(result)
finish = time.perf_counter()
print(f'Finished in {round(finish-start, 2)} second(s)')
这里是工作,带有内联注释-当在windows下进行类似分叉的行为时,需要防止再次调用main((。
这只在windows下是必要的,因为windows不支持fork——因此python模拟了一些"fork";分叉状";行为,并试图在新流程中创建相同的环境:
import concurrent.futures
import time
def do_something(seconds):
print(f'Sleeping {seconds} second(s)...')
time.sleep(seconds)
return f'Done Sleeping...{seconds}'
def main():
start = time.perf_counter()
with concurrent.futures.ProcessPoolExecutor() as executor:
secs = [5, 4, 3, 2, 1]
results = executor.map(do_something, secs)
# for result in results:
# print(result)
finish = time.perf_counter()
print(f'Finished in {round(finish-start, 2)} second(s)')
if __name__ == '__main__':
# this guards main() when forking
main()