简体   繁体   English

每x分钟运行python作业

[英]Run python job every x minutes

I have a small python script that basically connects to a SQL Server (Micrsoft) database and gets users from there, and then syncs them to another mysql database, basically im just running queries to check if the user exists, if not, then add that user to the mysql database. 我有一个小的python脚本,基本上连接到SQL Server(Micrsoft)数据库并从那里获取用户,然后将它们同步到另一个mysql数据库,基本上我只是运行查询来检查用户是否存在,如果没有,然后添加用户到mysql数据库。

The script usually would take around 1 min to sync. 该脚本通常需要大约1分钟才能同步。 I require the script to do its work every 5 mins (for example) exactly once (one sync per 5 mins). 我要求脚本每5分钟(例如)完成一次(每5分钟一次同步)。

How would be the best way to go about building this? 如何构建这个最好的方法?

I have some test data for the users but on the real site, theres a lot more users so I can't guarantee the script takes 1 min to execute, it might even take 20 mins. 我有一些用户的测试数据,但在真实的网站上,有更多的用户,所以我不能保证脚本需要1分钟执行,甚至可能需要20分钟。 However having an interval of say 15 mins everytime the script executes would be ideal for the problem... 但是,每次脚本执行时间隔为15分钟都是理想的问题...

Update: I have the connection params for the sql server windows db, so I'm using a small ubuntu server to sync between the two databases located on different servers. 更新:我有sql server windows db的连接参数,所以我使用一个小的ubuntu服务器在位于不同服务器上的两个数据库之间进行同步。 So lets say db1 (windows) and db2 (linux) are the database servers, I'm using s1 (python server) and pymssql and mysql python modules to sync. 所以说db1(windows)和db2(linux)是数据库服务器,我使用s1(python服务器)和pymssql和mysql python模块进行同步。

Regards 问候

I am not sure cron is right for the job. 我不确定cron是否适合这项工作。 It seems to me that if you have it run every 15 minutes but sometimes a synch takes 20 minutes you could have multiple processes running at once and possibly collide. 在我看来,如果你每15分钟运行一次,但有时同步需要20分钟,你可能会同时运行多个进程并可能发生冲突。

If the driving force is a constant wait time between the variable execution times then you might need a continuously running process with a wait. 如果驱动力是变量执行时间之间的恒定等待时间,那么您可能需要持续运行的进程并等待。

def main():
    loopInt = 0
    while(loopInt < 10000):
        synchDatabase() 

        loopInt += 1
        print("call #" + str(loopInt))
        time.sleep(300)  #sleep 5 minutes

main()

(obviously not continuous, but long running) You can set the result of while to true and it will be continuous. (显然不是连续的,但是长时间运行)你可以将while的结果设置为true,它将是连续的。 (comment out loopInt += 1 ) (注释掉loopInt += 1

Edited to add: Please see note in comments about monitoring the process as you don't want the script to hang or crash and you not be aware of it. 编辑添加:请参阅有关监视进程的注释中的注释,因为您不希望脚本挂起或崩溃,并且您不了解它。

You might want to use a system that handles queues, for example RabbitMQ, and use Celery as the python interface to implement it. 您可能希望使用处理队列的系统,例如RabbitMQ,并使用Celery作为python接口来实现它。 With Celery, you can add tasks (like execution of a script) to a queue or run a schedule that'll perform a task after a given interval (just like cron ). 使用Celery,您可以将任务(如执行脚本)添加到队列中,或者运行在给定时间间隔后执行任务的计划(就像cron一样)。

Get started http://celery.readthedocs.org/en/latest/ 开始使用http://celery.readthedocs.org/en/latest/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM