如何在烧瓶应用程序运行时每天00:30增加变量?

时间:2015-05-12 18:44:51

标签: python multithreading flask

我有一个python flask应用程序,它开始运行一个名为' counter'的变量。初始化为0。 我想在00:30 AM每天递增此变量并将其打印到python控制台,而不会干扰应用程序运行。

我可能希望将该变量的当前值随时传递给客户端。

counter = 0

if __name__ == "__main__":
   port = 80
   os.system("open http://localhost:{0}".format(port))

   app.debug = False
   app.run(host='0.0.0.0', port=port)

   #### NEED CODE FOR HERE ####
   #Trying out logic:
   if (time == 00:30):
       counter = counter+1
       print counter
   #### NEED CODE FOR HERE ####

请在答案中提供工作代码。

2 个答案:

答案 0 :(得分:2)

There are several solutions in other answers, depending on how complex your requirements are. If all you really need to do is increment a counter, then use the first approach.

If you have the ability to schedule cron jobs on your server, the simplest approach by far is to define a URL that performs the action, and have a cron job that runs public void itemInsertResponse(Intent data) { String item = data.getStringExtra("item"); String location = data.getStringExtra("location"); TableLayout itemCatalog = (TableLayout) findViewById(R.id.tbl_catalog); itemCatalog.addView(createItemRow(item, location), new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT)); } public TableRow createItemRow(String item, String location) { TableRow itemRow = new TableRow(this); itemRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT)); TextView itemText = new TextView(this); TextView locationText = new TextView(this); itemText.setText(item); itemText.setId(3); locationText.setId(4); locationText.setText(location); itemRow.addView(itemText); itemRow.addView(locationText); //setPadding appears to take values in px, need to conver to dp int padding_in_dp = 5; // 6 dps final float scale = getResources().getDisplayMetrics().density; int padding_in_px = (int) (padding_in_dp * scale + 0.5f); itemRow.setPadding(padding_in_px, padding_in_px, padding_in_px, padding_in_px); itemRow.setId(9001); return itemRow; } or curl to request (with wget) the URL and trigger the action at the required time. This requires no additional packages and is very lightweight. Much as described here:

You could possibly use a background thread, which in your case would need to sleep or set a timer to wake up at the required time:

One of the most popular options is to use Celery for background task processing. This is rather heavyweight as it also requires RabbitMQ, but is the most flexible and powerful solution:

There's Python Redis-Queue, which requires the Redis backend:

Some good ideas on Reddit also:

An overview of task queues for Python:

答案 1 :(得分:0)

最简单的方法是根本没有计数器,而是在需要时动态计算计数器的值。

例如,如果您希望计数器在2015年5月12日0:30从0开始,则可以使用此代码获取随后的任何时间的计数器值:

import datetime

start_counter = datetime.datetime(2015, 5, 12, 0, 30)

def get_counter(when):
    return (when - start_counter).days

以下示例显示了计数器在不同时间的值:

>>> get_counter(datetime.datetime(2015,5,12,0,45))  # May 12th, 0:45am
0
>>> get_counter(datetime.datetime(2015,5,13,0,45))  # May 13th, 0:45am
1
>>> get_counter(datetime.datetime(2015,5,14,0,15))  # May 14th, 0:15am
1
>>> get_counter(datetime.datetime(2015,5,14,0,45))  # May 14th, 0:45am
2

我希望这有帮助!