簡體   English   中英

將 Magic 命令與 python 多處理庫一起使用

[英]Using Magic command with python Multiprocessing library

我正在嘗試為來自另一個筆記本的 python 列表中的每個輸入運行 Jupyter 筆記本文件我使用 Jupyter Notebook 的魔術命令%run來完成任務

input_list= [1,  131,  312,  327,  348,  485,  469, 1218, 1329, 11212]
for i in input_list:
    try:
        input = i
        !run ./notebook.ipynb 
    except:
        pass

代碼正在運行,但執行時間非常長所以我決定將多處理庫與代碼一起使用以更快地執行代碼

使用內部多處理的函數

def function(i):
    try:
        input = i
        print(input)#print the current element passed
        %run ./notebook.ipynb
    except:
        pass

多處理代碼

    from multiprocessing import Pool, cpu_count
    from tqdm import tqdm

    p = Pool(8)

    tqdm(p.imap(function, input_list))

    p.close()
    p.join()

但是這里的問題是傳遞給 Function 的參數沒有傳遞給 %run 魔術命令中使用的筆記本

我收到類似“未定義輸入”的錯誤

這個問題的可能解決方案是什么?

當您按照此處的指南了解如何使用參數時,它就會起作用。
用一個最小的工作示例來說明。

制作一個名為add3.ipynb的筆記本,其中唯一的單元格包含以下內容:

o = i + 3
print (f"where the input is {i}; the  output is {o}\n")

然后,為了讓您的筆記本使用您想要的各種值來控制運行,請在代碼單元中使用以下內容:

# based on https://pymotw.com/3/multiprocessing/basics.html
import multiprocessing

def worker(i):
    try:
        print (f"input is {i}\n")#print the current element passed
        %run ./add3.ipynb
    except:
        pass
    

input_list= [1,  131,  312,  327,  348,  485,  469, 1218, 1329, 11212]


if __name__ == '__main__':
    jobs = []
    for i in input_list:
        p = multiprocessing.Process(target=worker, args=(i,))
        jobs.append(p)
        p.start()

我將在這篇文章的底部粘貼一個典型的運行。


我仍然建議您使用papermill來執行此操作,以便您可以參數化筆記本,然后將文件與新版本一起保存,就像報告一樣。

或者,您可以使用其他方式注入代碼或構建筆記本以使用輸入值運行。 很多時候,我在腳本內部使用字符串中的模板,並用占位符表示值。 然后我運行腳本以使用string.replace()方法生成具有其中值的筆記本,將生成的字符串保存為筆記本文件,然后使用jupytextjupyter nbconvert運行這些筆記本。 nbformat對於構建這樣的筆記本文件也很有用。 這樣,您就可以生成筆記本形式的報告,其中包含每次運行的結果。

此外,如果您不需要將調用的代碼保存在筆記本中,則將其保存為 python 腳本(以.py結尾)或 ipython 腳本(以.ipy結尾)通常更方便。 (后者允許您在腳本中使用 IPython 魔法,並且當您習慣了 Jupyter 時,這通常是一種更簡單的開發方式。但是,生成的腳本運行速度比純 Python 慢得多,所以我通常最終轉換為純 Python,並且只在開發早期使用.ipy形式。)例如,我的示例add3.ipynb中的一個單元格的內容可能只是一個腳本add3.py保存。 然后在筆記本中我可以像下面這樣運行它(為了簡單起見,省略了多處理):

input_list= [1,  131,  312,  327,  348,  485,  469, 1218, 1329, 11212]
for i in input_list:
    %run -i add3.py

請注意使用-i選項和%run來“在 IPython 的命名空間中運行文件,而不是在空的命名空間中運行文件”。 請注意,當使用%run運行另一個筆記本時,該選項不是必需的,因為默認情況下,就好像您在調用筆記本時運行另一個筆記本一樣。 我喜歡將%run與腳本結合使用的更大靈活性,因為我通常不希望腳本在同一個命名空間中運行。 我提到的替代方案(papermill、jupytext 和 &jupyter nbconvert)來執行與當前命名空間分開的外部筆記本。


運行最小工作示例時看到的結果:

input is 1

input is 131

input is 312
input is 327


input is 348
input is 485


input is 469
input is 1218


input is 11212
input is 1329

where the input is 131; the  output is 134


where the input is 1; the  output is 4
where the input is 312; the  output is 315
where the input is 327; the  output is 330


where the input is 485; the  output is 488


where the input is 1218; the  output is 1221
where the input is 469; the  output is 472


where the input is 348; the  output is 351

where the input is 1329; the  output is 1332

where the input is 11212; the  output is 11215

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM