Can we add jobs to running schedular in ApSchedular - python

I started Background scheduler in one file and ran it. Then from other file I accessed scheduler instance and added job. What my thought was the instance will add the job and it will run. I am new to this scheduling mechanisms. What I did is
On one file Main.py
import time
from apscheduler.schedulers.background import BackgroundScheduler
class Main:
a = 2
sched = BackgroundScheduler()
sched.start()
while True:
time.sleep(5)
From other file Bm.py
from Main import Main
class Bm(Main) :
def timed_job():
print 'aa'
Main.sched.add_job(timed_job,'interval',seconds=1)
I thought this would do, but it didnot.I need to this way from seperate file because I need to make a task manager which would run jobs and I need to be able to add or remove jobs anytime needed.SO how can we add and remove jobs to/from running apscheduler?
UPDATE :
This is confusing. I added a function printme on Main.py and did sched.add_job(printme,'interval',seconds=5), it prints me as expected but when I run Bm.py it also prints me, when it was supposed to print aa
def printme():
print 'me'
while True:
# time.sleep(5)
sched.add_job(printme,'interval',seconds=5)
if (input() is 'q'):
sched.shutdown()

Related

Using ThreadPoolExecutor to run ongoing task as well as another separate service

Is it possible to use ThreadPoolExecutor to run tasks in the background?
I want to map an ongoing listener to a task and then run another service after this.
I can achieve that with threading.Thread, but not ThreadPoolExecutor
i.e. this works:
# set a custom thread name prefix for all threads in the pool
import threading
from concurrent.futures import ThreadPoolExecutor
import time
# a mock task that does nothing
def task(name):
i = 0
while 1:
print(i)
i = i + 1
time.sleep(5)
threading.Thread(target=lambda: task("test")).start()
print("to here")
and gives
0to here
1
2
Where as if I use this:
with ThreadPoolExecutor(thread_name_prefix='TaskPool') as executor:
executor.map(task("test"))
print("to here")
We never reach "to here"
How can I configure ThreadPoolExecutor appropriately?

Creating a Flag file

I'm relatively new to python so please forgive early level understanding!
I am working to create a kind of flag file. Its job is to monitor a Python executable, the flag file is constantly running and prints "Start" when the executable started, "Running" while it runs and "Stop" when its stopped or crashed, if a crash occurs i want it to be able to restart the script. so far i have this down for the Restart:
from subprocess import run
from time import sleep
# Path and name to the script you are trying to start
file_path = "py"
restart_timer = 2
def start_script():
try:
# Make sure 'python' command is available
run("python "+file_path, check=True)
except:
# Script crashed, lets restart it!
handle_crash()
def handle_crash():
sleep(restart_timer) # Restarts the script after 2 seconds
start_script()
start_script()
how can i implement this along with a flag file?
Not sure what you mean with "flag", but this minimally achieves what you want.
Main file main.py:
import subprocess
import sys
from time import sleep
restart_timer = 2
file_path = 'sub.py' # file name of the other process
def start():
try:
# sys.executable -> same python executable
subprocess.run([sys.executable, file_path], check=True)
except subprocess.CalledProcessError:
sleep(restart_timer)
return True
else:
return False
def main():
print("starting...")
monitor = True
while monitor:
monitor = start()
if __name__ == '__main__':
main()
Then the process that gets spawned, called sub.py:
from time import sleep
sleep(1)
print("doing stuff...")
# comment out to see change
raise ValueError("sub.py is throwing error...")
Put those files into the same directory and run it with python main.py
You can comment out the throwing of the random error to see the main script terminate normally.
On a larger note, this example is not saying it is a good way to achieve the quality you need...

Schdeuling jobs after calling .start() APscheduler

I am writing a program to schedule and cancel alarms in Flask. I am using the apscheduler library for the timings.
I need to be able to add events to the job queue at any point, so I need to be able to add events after the scheduler is run.
Currently, I have:
from apscheduler.schedulers.background import BackgroundScheduler
def cancel():
job = events[0]
job.remove()
def schedule():
sched = scheds[0]
try:
sched.shutdown()
except:
pass
job = sched.add_job(my_job, 'date', run_date=t, args=['text'])
events.append(job)
sched.start()
def schedule2():
sched = scheds[0]
try:
sched.shutdown()
except:
pass
job = sched.add_job(my_job, 'date', run_date=t2, args=['text'])
events.append(job)
sched.start()
Where scheds is an array to store a global scheduler, and events is an array which stores the events that are scheduled.
I need to run schedule, then schedule2, to schedule two different jobs. When I try this, I get an error which says that I cannot run schedule2 because the 'scheduler is already running'. How can I achieve this?

web2py scheduling multiple tasks

in my web2py app i am using scheduler. So far I have one task scheduled which runs a subprocess when called from controler (an external exe file/application)
Now i want to add another task which will do some background work
My code in scheduler.py till now was
def runWoshiEngine(scriptId, path):
# import os, sys
# import time
import subprocess
print "runWoshiEngine in progress......"
p = subprocess.Popen(['woshi_engine.exe', scriptId], shell=True, stdout = subprocess.PIPE, cwd=path)
return dict(status = 1)
from gluon.scheduler import Scheduler
scheduler = Scheduler(db, heartbeat = 1)
so this way the scheduler started on client's request
after starting my app I run my scheduler with command
python web2py.py -K myapp
Now I want to add another function which will start every hour to do some background work.
What would you recommend and how to add it to scheduler because if i add anything to my line of code my initial task --> exe app is not started
thank you
best regards

Python BackgroundScheduler program crashing when ran from another module

I am trying to build a application that will run a bash script every 10 minutes. I am using apscheduler to accomplish this and when i run my code from terminal it works like clock work. However when i try to run the code from another module it crashes i suspect that the calling module is waiting for the "schedule" module to finish and then crash when that never happens.
Error code
/bin/bash: line 1: 13613 Killed ( python ) < /tmp/vIZsEfp/26
shell returned 137
Function that calls schedule
def shedual_toggled(self,widget):
prosessSchedular.start_background_checker()
Schedule Program
def schedul_check():
"""set up to call prosess checker every 10 mins"""
print "%s check ran" %(counter)
counter =+ 1
app = prosessCheckerv3.call_bash() < calls the bash file
if app == False:
print "error with bash"
return False
else:
prosessCheckerv3.build_snap_shot(app)
def start_background_checker():
scheduler = BackgroundScheduler()
scheduler.add_job(schedul_check, 'interval', minutes=10)
scheduler.start()
while True:
time.sleep(2)
if __name__ == '__main__':
start_background_checker()
this program simply calls another ever 10 mins. As a side note i have been trying to stay as far away from multi-threading as possible but if that is required so be it.
Well I managed to figure it out my self. The issue that GTK+ is not thread safe so the timed module need to be either be ran in another thread or else you can realise/enter the thread before/after calling the module.
I just did it like this.
def shedual_toggeld(self,widget):
onOffSwitch = widget.get_active()
""" After main GTK has logicly finished all GUI work run thread on toggel button """
thread = threading.Thread(target=self.call_schedual, args=(onOffSwitch,))
thread.daemon = True
thread.start()
def call_schedual(self, onOffSwitch):
if onOffSwitch == True:
self.sch.start_background_checker()
else:
self.sch.stop_background_checker()
This article goes through it in more detail. Hopefully some one else will find this useful.
http://blogs.operationaldynamics.com/andrew/software/gnome-desktop/gtk-thread-awareness

Categories