从3.7.6
升级到3.8.7
。
import multiprocessing as mp
from multiprocessing import Process
from time import sleep
def back_sleep(): sleep(5)
if __name__ == '__main__':
proc = multiprocessing.Process(target=back_sleep, daemon=True)
proc.start()
错误我似乎无法绕过。其他解决方案指向池。尝试设置Pool()
...spawn.py", line 126, in _main self = reduction.pickle.load(from_parent)
AttributeError: Can't get attribute 'back_sleep' on <module '__main__' (built-in)>
我希望我的shell/内核在后台进程(cpu密集型任务)运行一段时间时立即可供用户使用。
在单独的文件(back_sleep.py)中:
from time import sleep
def back_sleep(sec: int):
sleep(sec)
print('hey')
sec = 2
back_sleep(sec)
在另一个文件(main.py)中:
import subprocess
subprocess.Popen([
"python3",
"back_sleep.py",
])
然后运行main.py文件