1

I want to run my program in jupyter notebook and this program stops at specific time(for example 18:00). I wrote program by while loop and incremental index, but it's better to write it with time parameter.

I run mentioned program for 7 hours each day. It must run nonstop.

while(i<500000):
    execute algorithm
    i+=1

But I'd like to run my program like bellow:

while(not 18:00 clock):
    execute algorithm
1
  • Hello there and welcome, Can you add more technical details to your question to help reproduce the issue Commented Mar 21, 2019 at 19:15

7 Answers 7

4

You can create a child process that will terminate parent process and itself at certain time:

import multiprocessing as mp
import time
import datetime
import sys
import signal
import os

def process(hr, minute):
    while True:
        d = datetime.datetime.now()
        if d.hour == hr and d.minute == minute:
            os.kill(os.getppid(), signal.SIGTERM)
            sys.exit()
        else:
            time.sleep(25)


p = mp.Process(target=process, args=(18, 0))
p.start()

# your program here ...
Sign up to request clarification or add additional context in comments.

Comments

2

Use:

import datetime
#create the alarm clock.
alarm = datetime.time(15, 8, 24) #Hour, minute and second you want.

On while:

while alarm < datetime.datetime.now().time():
    do something

You could set a specific date too, setting like this:

datetime.datetime(2019, 3, 21, 22, 0, 0)  #Year, month, day, hour, minute and second you want.

For more info, check the documentation of datetime.

Comments

1
import datetime

while datetime.datetime.now().hour < 18:
    do stuff...

or

if datetime.datetime.now().hour >= 18:
    return

Comments

1

You could create a function that takes hour and minutes as parameters and to perform a check inside the while loop:

import datetime

def proc(h, m):
    while True:
        currentHour = datetime.datetime.now().hour
        currentMinute = datetime.datetime.now().minute
        if currentHour == h and currentMinute == m:
            break
        # Do stuff...

# Function call.
proc(18,0)

Comments

0

import datetime

https://docs.python.org/3/library/datetime.html

You can then use the various functions (time or timedelta) to set a time.

timeNow = datetime.datetime() print timeNow

Comments

0

You could set this up as a cron job and start the job at time x and stop at time x.

Comments

0

Let us assume you want you code to run 10 pm(22:oo) hour every day. If you are using Linux, you can do something like this to run job as root user

sudo crontab -e 
0 22 * * *  /path/to/directory/python my_code.py

your python file my_code.py could be something like that

# python code to search pattern in a string using regex
import re

str1 = 'this is {new} string with [special] words.'

r = re.search(r'\{(.*?)\}', str1)
if r:
    found =r.group()
else:
    "No match found"

print found

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.