def run(): executors = { 'default': { 'type': 'threadpool', 'max_workers': 10 }, 'processpool': ProcessPoolExecutor(max_workers=5) } scheduler = BlockingScheduler() scheduler.configure(executors=executors) client = api.use('ths', debug=False) client.connect(r"c:\\workspace\\同花顺\\\\xiadan.exe", timeout=5) client.enable_type_keys_for_editor() # add job for computing trendency of all stock scheduler.add_job(join_quant_follower_sell, 'cron', day_of_week='mon-fri', hour=9, minute=27, args=[client]) scheduler.add_job(join_quant_follower_buy, 'cron', day_of_week='mon-fri', hour=9, minute=31, args=[client]) # join_quant_follower_sell(client,session) # join_quant_follower_buy(client,session) try: scheduler.start() except (KeyboardInterrupt, SystemExit): scheduler.remove_all_jobs()
def run(): executors = { 'default': { 'type': 'threadpool', 'max_workers': 10 }, 'processpool': ProcessPoolExecutor(max_workers=5) } scheduler = BlockingScheduler() scheduler.configure(executors=executors) start = datetime.today() - timedelta(days=45) start = start.strftime('%Y-%m-%d') # source_dir = 'data/industry_sw/' industry = 'all' max_file_count = 1000 seq_dim = 20 input_dim = 5 out_dim = 8 #add job for computing trendency of all stock scheduler.add_job( stock_model_sys_v2.select_best_stock_at_yestoday, 'cron', day_of_week='mon-fri', hour=3, minute=0, args=[start, industry, max_file_count, seq_dim, input_dim, out_dim]) #select best stock when matket has opened scheduler.add_job( stock_model_sys_v2.select_best_stock_after_open, 'cron', day_of_week='mon-fri', hour=9, minute=26, args=[start, industry, max_file_count, seq_dim, input_dim, out_dim]) try: scheduler.start() except (KeyboardInterrupt, SystemExit): scheduler.remove_all_jobs()
''' # TODO 计划任务 周一到周五21:39执行 2017-6-30开始,2017-9-30停止 job = sched.add_job(job_fun, 'cron', day_of_week='mon-fri', hour=21, minute=39, start_date='2017-06-30' ,end_date='2017-09-30') ''' #TODO 获得任务属性和状态 sched.print_jobs() #TODO 获得任务id和执行函数 print(sched.get_jobs()) #TODO 删除任务不执行 #job.remove() ''' #TODO 暂停以及恢复任务 job2.pause() job2.resume() ''' print("press Ctrl+{} TO STOP TASK".format('break' if platform.system() == 'Windows' else 'C')) try: sched.start() except (SyntaxWarning, BlockingIOError, SystemError) as e: print(e) except (KeyboardInterrupt, SystemExit): print('clean the job') #TODO 获得任务id和执行函数 print(sched.get_jobs()) taskid = str(input('input id')) sched.remove_job(taskid) if taskid.isspace() or taskid.isidentifier(): sched.remove_all_jobs()
class JobLauncher(object): def __init__(self, background=False, deamon=True, **kwargs): logging.basicConfig(format="[%(asctime)s] %(message)s", atefmt="%Y-%m-%d %H:%M:%S") logging.getLogger('apscheduler').setLevel(logging.DEBUG) if background: self.sched = BackgroundScheduler(deamon=deamon) # background else: self.sched = BlockingScheduler(deamon=deamon) # foreground # TODO: Read from configuration file. self.sched.configure( jobstores={ # "sqlite": SQLAlchemyJobStore(url='sqlite:///app/database/example.db'), # "default": MemoryJobStore() "default": SQLAlchemyJobStore(url='sqlite:///app/database/example.db') }, executors={ 'default': ThreadPoolExecutor(20), 'processpool': ProcessPoolExecutor(5) }, job_defaults={ 'coalesce': False, 'max_instances': 3 }, timezone=get_localzone() # Asia/Seoul ) self.retried = 0 self.logger = logging.getLogger('apscheduler') super(JobLauncher, self).__init__() def start(self): try: if self.sched.state != STATE_RUNNING: self.printJobs(jobstore='default') started = self.sched.start() except ConflictingIdError as e: traceback.print_exc() except KeyboardInterrupt as e: traceback.print_exc() finally: pass # Remove all remained store. # self.sched.remove_all_jobs() # for job in self.getJobs(): # if job.pending: # job.pause() self.logger.info('Finished') self.logger.info(self.getJobs()) self.printJobs() def stop(self, wait=False): if self.sched.state == STATE_RUNNING: self.sched.shutdown(wait=wait) def resume(self): if self.sched.state == STATE_RUNNING: self.sched.resume() def pause(self): if self.sched.state == STATE_RUNNING: self.sched.pause() def addListener(self, listener, types): self.sched.add_listener(listener, types) def addJob(self, job, **kwargs): execute, trigger, options = job.build(**kwargs) added_job = self.sched.add_job(execute, trigger, **options) self.printJobs() return added_job def getJob(self, job_id): return self.sched.get_job(job_id) def getJobs(self, jobstore=None): return self.sched.get_jobs(jobstore=jobstore) def removeJob(self, job_id, jobstore=None): return self.sched.remove_job(job_id, jobstore=jobstore) def removeAllJob(self, jobstore=None): return self.sched.remove_all_jobs(jobstore=jobstore) def printJobs(self, jobstore=None, out=None): return self.sched.print_jobs(jobstore=jobstore, out=None) def getJobState(self, job_id=None, jobstore=None): state = list() if job_id is not None: job = self.sched.get_job(job_id, jobstore=jobstore) if job is not None: temp = dict() temp[job.id] = { "next_run_time": job.next_run_time, "state": job.pending, } state.append(temp) else: for job in self.sched.get_jobs(jobstore=jobstore): temp = dict() temp[job.id] = { "next_run_time": job.next_run_time, "state": job.pending, } state.append(temp) return state