900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Windows下 jupyter notebook 运行multiprocessing 报错的问题与解决方法

Windows下 jupyter notebook 运行multiprocessing 报错的问题与解决方法

时间:2024-04-29 00:56:26

相关推荐

Windows下 jupyter notebook 运行multiprocessing 报错的问题与解决方法

文章目录

测试用的代码错误解决方法

测试用的代码

下面每一个对应一个jupyter notebook的单元格

import timefrom multiprocessing import Process, Queue

def generator():c = 0while True:time.sleep(1.0) # read somethingyield cc += 1

%%timeds = generator()for i in range(3):item = next(ds)time.sleep(1.0) # do somethingprint(item)

012CPU times: user 3.27 ms, sys: 456 µs, total: 3.73 msWall time: 6.01 s

def kernel(func, q: Queue):ds = func()while True:item = next(ds)q.put(item)

def multi_generator(func):q = Queue()p = Process(target=kernel, args=(func, q))p.start()while True:item = q.get()yield item

在windows下运行这个单元格的话,会一直显示在运行,

%%timeds = multi_generator(generator)for i in range(3):item = next(ds)time.sleep(1.0)print(item)

错误

运行上面的代码,后台会报如下错

Traceback (most recent call last):File "<string>", line 1, in <module>File "D:\Anaconda\envs\AIE31\lib\multiprocessing\spawn.py", line 105, in spawn_mainexitcode = _main(fd)File "D:\Anaconda\envs\AIE31\lib\multiprocessing\spawn.py", line 115, in _mainself = reduction.pickle.load(from_parent)AttributeError: Can't get attribute 'kernel' on <module '__main__' (built-in)>

解决方法

1、些处解决方案,是利用Windows 10 下的WSL的Linux系统解决的。

怎样配置WSL,请参考链接/jasneik/article/details/12378

直接打开Ubuntu 控制台,运行jupyter notebook,一般不会像Windows下的会自动跳转,可以把jupyter notebook含token的链接拷贝到浏览器打开。类似如下的链接

然后把上面的代码拷,再运行,就OK了。

%%timeds = multi_generator(generator)for i in range(3):item = next(ds)time.sleep(1.0)print(item)

012CPU times: user 9.26 ms, sys: 1.38 ms, total: 10.6 msWall time: 4.06 s

2、可以参考此方面,是把方法写到临时文件(我没有试过)

/e274794140/article/details/87286190

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。