Python 如何安排用户每分钟运行函数库

Python 如何安排用户每分钟运行函数库,python,django,django-rest-framework,schedule,Python,Django,Django Rest Framework,Schedule,当我使用计划库每1分钟运行一次函数时。它工作正常,但我的另一个URL不工作。只有预定的功能工作 #import scheduler library import schedule import time #update every hour global table no of players and totol payouts def updateTotalPayoutsAndTotalPlayers(): try: querysetGlobal = Glob

当我使用计划库每1分钟运行一次函数时。它工作正常,但我的另一个URL不工作。只有预定的功能工作

#import scheduler library
import schedule
import time    

#update every hour global table no of players and totol payouts
def updateTotalPayoutsAndTotalPlayers():
    try:
        querysetGlobal = Global.objects.filter().first()
        querysetUser = Players.objects.filter(is_active=1).count()
        querysetTotalPayouts = Game.objects.filter(gameStatusId=3).aggregate(Sum('pot'))
        totalPayouts = querysetTotalPayouts['pot__sum']
        # print("total payouts : ",totalPayouts)
        querysetGlobal.totalPlayers = querysetUser
        querysetGlobal.totalPayouts = 0 if totalPayouts==None else totalPayouts

        querysetGlobal.save()
        # print("completed task......")

    except Exception as e:
        print("error updating payouts and no of players : ", e) 


#update globals table called ever one minute
schedule.every(1).minutes.do(updateTotalPayoutsAndTotalPlayers)

while True:
    schedule.run_pending()
    time.sleep(1) 
当我在
其他API工作时删除了。但是我的日程安排不行

要运行服务器,我使用命令:python manage.py runserver

from celery import shared_task
@shared_task(bind=True, default_retry_delay=10, max_retries=5)
    def my_func(self):
        ***some code***
或者尝试类似的方法:

@periodic_task(run_every=timedelta(seconds=60))
def my_func():
    ***some code***
在此之前,请在Linux/Windows/Mac上安装redis服务器 并让pip安装redis

在您的设置中:

# Redis/Celery application definition
REDIS_HOST = '127.0.0.1'
REDIS_PORT = '6379'
BROKER_URL = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'
BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600}
CELERY_RESULT_BACKEND = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'

你可能想看看芹菜,这是为了运行定期任务而设计的。另外,请看一看未定义的定期任务。当从芹菜中导入时,会显示无法导入周期性任务仍然出现错误:未定义名称“周期性任务”,请共享文档链接。pip安装芹菜