Jupyter notebook, how to run multiple cells simultaneously?

2024/9/21 3:27:07

I defined a python function which run a bash script. Let's say the function is: calc(x,y,z). If I run this function in python with some variables,

>>> calc(1,2,3)

It generates a C code which simulate something using the variables (x=1, y=2, z=3), compiles the C code and executes the compiled output file.

I want to run multiple calc(x,y,z)s with different (x,y,z)s in jupyter notebook simultaneously. As you may noticed, the problem is that cells in jupyter notebook are executed sequentially. If I run three calc functions, it takes three times longer than the one function running time.

I tried two ways but they didn't work well.

  1. Use multiprocessing module: By using the module, it is possible to execute multiple calcs simultaneously in "one cell". But for later analysis, I would like to execute multiple cells simultaneously which include only one calc each using different processors (or cpu cores).
  2. Use ipyparallel cell magic (inspired by this answer): I tried as following after import ipyparallel

    # Cell 1
    %%px --targets 0 # use processor 0
    calc(1,1,1)
    

    .

    # Cell 2
    %%px --targets 1 # use processor 1
    calc(2,2,2)        
    

    .

    # Cell 3
    %%px --targets 2 # use processor 2
    calc(3,3,3) 
    

But the cells are executed sequentially: Cell 2 is executed after Cell 1 simulation was done, similar for Cell 3.

How to run multiple jupyter cells using different cores?

Answer

In your solution, the cells are executed by different engines as you expected. The issue is caused by default blocking behavior. You can simply add --noblock argument to execute a cell in non-blocking mode. Then the cell returns AsyncResult object that makes it possible to read the output once the execution is finished by calling method display_outputs(). Go to the documentation for details targets-and-blocking

# Cell 1
%%px --targets 0 --noblock
calc(1,1,1)

.

# Cell 2
%%px --targets 1 --noblock
calc(2,2,2)   

.

# Cell 3
%%px --targets 2 --noblock
calc(3,3,3) 

If you need to access the output you can call display_outputs() as I explained above.

# output of the first the cell 1
___.display_outputs()# output of the first the cell 2
__.display_outputs()# output of the first the cell 1
_.display_outputs()

I used underscore notation to access AsyncResult objects returned by the cells 1-3. There is a number of other ways to access those objects, for example by using Out[x] where x is the cell's execution number visible in the notebook after executing a cell.

https://en.xdnf.cn/q/72272.html

Related Q&A

How to make a slice of DataFrame and fillna in specific slice using Python Pandas?

The problem: let us take Titanic dataset from Kaggle. I have dataframe with columns "Pclass", "Sex" and "Age". I need to fill NaN in column "Age" with a median f…

Pythons difflib SequenceMatcher speed up

Im using difflib SequenceMatcher (ratio() method) to define similarity between text files. While difflib is relatively fast to compare a small set of text files e.g. 10 files of 70 kb on average compar…

create an asymmetric colormap

I am creating a colormap to map colors in a folium choropleth map, using code from here:from branca.colormap import linearcolormap = linear.RdBu.scale(df.MyValue.min(),df.MyValue.max())colormapAs you c…

NLTK - Get and Simplify List of Tags

Im using the Brown Corpus. I want some way to print out all the possible tags and their names (not just tag abbreviations). There are also quite a few tags, is there a way to simplify the tags? By sim…

PolynomialFeatures object has no attribute predict

I want to apply k-fold cross validation on the following regression models:Linear Regression Polynomial Regression Support Vector Regression Decision Tree Regression Random Forest RegressionI am able t…

Error module object has no attribute freetype

I am using this code Link but it displays error of module object has no attribute i tried to pip install freetype but nothing happened. Can anyone please guide me with this.import cv2 import numpy as …

Count total number of white pixels in an image

I am trying to count total number of white pixels in the following image:But with my code, I get this errorsrc is not a numpy array, neither a scalar.This is my code: img=cv2.imread(filename,1) TP= wid…

Pass a JSON object to an url with requests

So, I want to use Kenneth excellent requests module. Stumbled up this problem while trying to use the Freebase API.Basically, their API looks like that:https://www.googleapis.com/freebase/v1/mqlread?q…

jenkinsapi python - how to trigger and track the job result

I am using JenkinsAPI to trigger parametrized jobs. I am aware of the REST API that Jenkins use, but our setup does not allow that directly; so the main mean for me to trigger jobs is through this libr…

Django test parallel AppRegistryNotReady

I am trying to understand how to run django tests in parallel with in memory sqlite3.I have django app with that structure:gbookorder...tests__init__.pytest_a1.pytest_b1.pyutils.pytest_a1.py and test_b…