Search code examples
pythonpython-3.xwhile-loopjupyter-notebookschedule

run two file.py one after the other in jupyter notebook


I am using jupyter notebook.

I have this two files with two scripts: script1.py and script2.py

While file script1.py runs every minutes in a while loop, I would like to run script2.py 4 minutes after running script.1, in the same loop.

This is what I have so far: A script that runs script1.py every minute in a loop with one minute sleep time.

starttime=time.time()

while True:
     %run "script1.py"
     time.sleep(60.0 - ((time.time() - starttime) % 60.0))

Where can I add %run "script2.py" to this code?


Solution

  • Just check if it's been 4 minutes, and run script2 if it has:

    starttime=time.time()
    
     while True:
         %run "script1.py"
    
         if (time.time() - starttime) >= 240.0
             %run "script2.py"
    
         time.sleep(60.0 - ((time.time() - starttime) % 60.0))