gpt4 book ai didi

multithreading - 用于在 ipython/jupyter 笔记本中运行单元格的新线程

转载 作者:行者123 更新时间:2023-12-02 20:46:27 39 4
gpt4 key购买 nike

有时,运行单个单元格需要很长时间,而在运行时,我想在同一笔记本中编写和运行其他单元格,访问同一上下文中的变量。

是否有任何 ipython 魔法可以使用,这样当它添加到单元格时,运行该单元格将自动创建一个新线程并使用笔记本中的共享全局数据运行?

最佳答案

这可能不是答案,而是答案的方向。我没有看到这样的东西,但我对此也很感兴趣。

我目前的发现表明,人们需要定义它自己的自定义细胞魔法。很好的引用是文档中的自定义单元魔术部分以及我会考虑的两个示例:

这两个链接都将代码包装在一个线程中。这可能是一个起点。

更新: github 上的 ngcm-tutorial 有后台作业类的描述

##github.com/jupyter/ngcm-tutorial/blob/master/Day-1/IPython%20Kernel/Background%20Jobs.ipynb
from IPython.lib import backgroundjobs as bg
jobs = bg.BackgroundJobManager()

def printfunc(interval=1, reps=5):
for n in range(reps):
time.sleep(interval)
print('In the background... %i' % n)
sys.stdout.flush()
print('All done!')
sys.stdout.flush()

jobs.new('printfunc(1,3)')
jobs.status()

更新2:另一种选择:

from IPython.display import display
from ipywidgets import IntProgress

import threading

class App(object):
def __init__(self, nloops=2000):
self.nloops = nloops
self.pb = IntProgress(description='Thread loops', min=0, max=self.nloops)

def start(self):
display(self.pb)
while self.pb.value < self.nloops:
self.pb.value += 1
self.pb.color = 'red'

app = App(nloops=20000)

t = threading.Thread(target=app.start)

t.start()
#t.join()

关于multithreading - 用于在 ipython/jupyter 笔记本中运行单元格的新线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32081926/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com