-
First Check
Commit to Help
Example Codeimport rpyc
from apscheduler import events as sche_events
from apscheduler.executors.pool import ProcessPoolExecutor
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from apscheduler.schedulers.background import BackgroundScheduler
from rpyc.utils.server import ThreadedServer
from common import logger
from config import settings
from db.models.sys_schedule_job import ScheduleJobLog
from db.session import create_session
from schemas.request.job_schema import JobLog
class SchedulerService(rpyc.Service):
def exposed_add_job(self, func, *args, **kwargs):
return scheduler.add_job(func, *args, **kwargs)
def exposed_modify_job(self, job_id, jobstore=None, **changes):
return scheduler.modify_job(job_id, jobstore, **changes)
def exposed_reschedule_job(self, job_id, jobstore=None, trigger=None, **trigger_args):
return scheduler.reschedule_job(job_id, jobstore, trigger, **trigger_args)
def exposed_pause_job(self, job_id, jobstore=None):
return scheduler.pause_job(job_id, jobstore)
def exposed_resume_job(self, job_id, jobstore=None):
return scheduler.resume_job(job_id, jobstore)
def exposed_remove_job(self, job_id, jobstore=None):
return scheduler.remove_job(job_id, jobstore)
def exposed_get_job(self, job_id):
return scheduler.get_job(job_id)
def exposed_get_jobs(self, jobstore=None):
return scheduler.get_jobs(jobstore)
def exposed_print_jobs(self, jobstore=None):
return scheduler.print_jobs(jobstore)
def exposed_pause(self):
return scheduler.pause()
def exposed_resume(self):
return scheduler.resume()
def exposed_state(self):
return scheduler.state()
def exposed_start(self, **kwargs):
return scheduler.start(**kwargs)
def exposed_shutdown(self, **kwargs):
return scheduler.shutdown(**kwargs)Description
Operating SystemLinux, Windows Operating System DetailsNo response FastAPI Version0.70.0 Python VersionPython 3.7.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
|
Note sure about your case, but did you try startup and shutdown events? |
Beta Was this translation helpful? Give feedback.
-
thanks for reply, i had known about app_sub_processes = []
@app.on_event("startup")
async def init_connect():
logger.info("App startup event")
import subprocess
global app_sub_processes
app_sub_processes.append({
"name": "scheduler",
"command": "python main_scheduler_rpc_server.py",
"pid": None,
"process": None
})
for sub_proc in app_sub_processes:
proc = subprocess.Popen(sub_proc["command"])
sub_proc["process"] = proc
sub_proc["pid"] = proc.pid
logger.info(f"sub_process[{sub_proc['name']}]-[{proc.pid}] start")
if settings.SCHEDULER:
global scheduler, rpc_server_coon
import rpyc
try:
rpc_server_coon = rpyc.connect(settings.SCHEDULER_RPC_SERVER_IP, settings.SCHEDULER_RPC_SERVER_PORT,
config={"allow_all_attrs": True, "allow_pickle": True})
scheduler = rpc_server_coon.root
except ConnectionRefusedError as e:
logger.error("please start rpc server first")
@app.on_event("shutdown")
async def shutdown_connect():
global app_sub_processes
for sub_proc in app_sub_processes:
sub_proc["process"].terminate()
logger.info(f"sub_process [{sub_proc['name']}]-[{sub_proc['pid']}] closed")
logger.info(f"App shutdown event")it seems work in window but i don't test it deepper or in unix. in my opinion, i don't think |
Beta Was this translation helpful? Give feedback.
-
|
I'm curious why did you choose such a design path. No offence here but looking at a code, it is just not good. You try to start on startup event, from what I understand, completely independent service that is bind to some port. Please assume you start your uvicorn server or any other in many processes (using Anyway, good or bad, I suspect this is not If you want, you may try to change from |
Beta Was this translation helpful? Give feedback.
-
|
I know it is strange in ecology of python, for those which was maked for lighter application. What i need is building a complete and integrated app. In my situation, scheduler jobs is always modified with the change of business need and just like what i say, if in different processes, i need to restart them both, and for me or the other colleagues, it's easy to forget to restart one. In Anyway, thanks for you reply and remind, i just noticed that |
Beta Was this translation helpful? Give feedback.
I'm curious why did you choose such a design path. No offence here but looking at a code, it is just not good. You try to start on startup event, from what I understand, completely independent service that is bind to some port.
Please assume you start your uvicorn server or any other in many processes (using
workersparam), you will get errors, probably "address already in use" or similar. Another situation: you do have many containers, vms etc. with your fastapi instance. For each one you start process with scheduler. Is that desired? Startup/Shutdown may be although a good place to set up/initialize connection as you do it, but - for me - not for starting, you name it, another service t…