apsheduler python is not running as scheduled - python

The job_function isnt getting executed even once, even when i waited for more than 10 mins.
from apscheduler.schedulers.background import BackgroundScheduler
import send_mail
def job_function():
print("Hello World")
send_mail('abc#test.com')
sched = BackgroundScheduler()
sched.add_job(job_function, 'interval', minutes=1)
sched.start()

Based on this code, and this code only, the problem looks like that your program terminates before the time limit is reached.
Try adding this infinite loop at the end of your program which will prevent it to quit:
while True:
time.sleep(1000)
Then terminate your program with CTRL+C.

Related

Apscheduler keeps spawned processes alive for no reason

EDIT: I found the issue. It was a problem with PyCharm. I ran the .py outside of PyCharm and it worked as expected. In PyCharm I enabled "Emulate terminal in output console" and it now also works there...
Expectations:
Apscheduler spawns a thread that checks a website for something.
If the something was found (or multiple of it), the thread spawns (multiple) processes to download it/them.
After five seconds the next check thread spawns. While the other downloads may continue in the background.
Problem:
The spawned processes never stop to exist, which makes other parts of the code (not included) not work, because I need to check if the processes are done etc.
If I use a simple time.sleep(5) instead (see code), it works as expected.
No I cannot set max_instances to 1 because this will stop the scheduled job from running if there is one active download process.
Code:
import datetime
import multiprocessing
from apscheduler.schedulers.background import BackgroundScheduler
class DownloadThread(multiprocessing.Process):
def __init__(self):
super().__init__()
print("Process started")
def main():
print(multiprocessing.active_children())
# prints: [<DownloadThread name='DownloadThread-1' pid=3188 parent=7088 started daemon>,
# <DownloadThread name='DownloadThread-3' pid=12228 parent=7088 started daemon>,
# <DownloadThread name='DownloadThread-2' pid=13544 parent=7088 started daemon>
# ...
# ]
new_process = DownloadThread()
new_process.daemon = True
new_process.start()
new_process.join()
if __name__ == '__main__':
sched = BackgroundScheduler()
sched.add_job(main, 'interval', args=(), seconds=5, max_instances=999, next_run_time=datetime.datetime.now())
sched.start()
while True:
# main() # works. Processes despawn.
# time.sleep(5)
input()

How to terminate a loop early in in a thread?

I have a loop which makes a get request to a webservice to fetch data and do some stuff, but I want to 'manually' terminate the thread/event, which I achieved with the following example:
from threading import Event
exit = Event()
if external_condition():
exit.set()
for _ in range(mins):
fetch_data_and_do_stuff()
exit.wait(10) #wait 10 seconds
With that, the only thing that terminates it's the sleep time between loops. How can I also kill the loop so it doesn't keep running until it gets to the last iteration?
nvm i've solved it like this
from threading import Event
exit = Event()
if external_condition():
exit.set()
for _ in range(mins):
fetch_data_and_do_stuff()
if exit.wait(10):
break
the condition returns true when killed and also sleeps the 10 seconds, so it works
you have 2 options ,
kill the thread or process entirely
or making the loop's boolean false. going that way
you could use a global variable in this way: [Python 3.7] , run it to see
from threading import Thread
from time import sleep
global glob
glob=True
def threaded_function():
while glob:
print("\n [Thread] this thread is running until main function halts this")
sleep(0.8)
if __name__ == "__main__":
thread = Thread(target = threaded_function, args = ())
thread.start()
for i in range(4,0,-1):
print("\n [Main] thread will be terminated in "+str(i)+" seconds")
sleep(1)
glob=False
while True:
print("[Main] program is over")
sleep(1)

python apscheduler does nothing

I made the following ,but it doesn't print the time.
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
def tick():
print('Tick! The time is: %s' % datetime.now())
scheduler = BackgroundScheduler()
scheduler.add_job(tick,'interval',seconds=3)
print('starting')
scheduler.start()
print('stopped')
This is because your program is exiting before the interval has elapsed and needs to be kept alive at least until the first interval, consider using the following example:
while True:
#Thread activity here (time.sleep(2) for example)
or using other forms of activity to keep your main thread alive. Or just print out the time without this scheduling if that's what you really need.

How to stop/terminate a python script from running with BackgroundScheduler() of APScheduler

I'm trying to stop the while loop execution from inside the foo() function. I've tried exit() / sys.exit without success. How can I stop completely the execution of the program from inside the function?
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime, timedelta
import time
import sys
def foo(stop = False):
print('Function foo executed')
if stop:
sys.exit
scheduler = BackgroundScheduler()
dd = datetime.now() + timedelta(seconds=10)
scheduler.add_job(foo, 'date', run_date=dd, args=[True])
scheduler.start()
while True:
print('Inside the loop')
time.sleep(2)
Use psutil
import psutil
psutil.Process().terminate()
From the doc,
psutil.Process() If PID is omitted current process PID (os.getpid()) is used.
Be aware terminate will leave with exit code 0. If you want other exit code, you can use send_signal() or even kill()

APScheduler Background Scheduler Not working?

I am using background scheduler to schedule my jobs. When I am executing python script in the console the print statements are not executed. Is the scheduler being terminated? Below is my sample code
from apscheduler.schedulers.background import BackgroundScheduler
def my_task1():
print("Task 1")
def ny_task2():
print("Task 2")
if __name__=='__main__':
scheduler = BackgroundScheduler()
scheduler.add_job(my_task1, 'cron', id='my_task1', seconds=5)
scheduler.add_job(my_task1, 'cron', id='my_task1', seconds=10)
scheduler.start()
When I run the following script in the command line. I am not able to see the print statements in the console. Am I missing something?
You have selected a scheduler that runs in a background thread. Then you let the script exit. This is why nothing happens. The jobs have not had any time to be executed. Use BlockingScheduler instead if you want to keep the script running.
You can use while loop to keep it alive
from apscheduler.schedulers.background import BackgroundScheduler
def my_task1():
print("Task 1")
def ny_task2():
print("Task 2")
if __name__=='__main__':
scheduler = BackgroundScheduler()
scheduler.add_job(my_task1, 'cron', id='my_task1', seconds=5)
scheduler.add_job(my_task1, 'cron', id='my_task1', seconds=10)
scheduler.start()
while True:
time.sleep(1)

Categories

Resources