在我的项目中,我必须每小时在数据库中加载数据。我尝试了 celery 和 cron,但发现所有东西都非常复杂,并且总是会产生一些问题。我在 Windows 中使用 pycharm。我是 Django 的新手,只需要一个简单的解决方案来每小时运行以下命令。
这会在数据库中加载数据。
"python manage.py fetchfiles"
management/commands/fetchfiles
from django.core.management.base import BaseCommand, CommandError
from dashboard.models import airdata as tdata
import requests
import json
class Command(BaseCommand):
help = 'Fetches api data'
"""def add_arguments(self, parser):
none"""
def handle(self, *args, **options):
#for a in range(0,1578,10):
a = True
offno = 0
lst2=[]
dict1={}
while a == True:
api_key = "579b464db66ec23bdd000001cdd3946e44ce4aad7209ff7b23ac571b"
url = "https://api.data.gov.in/resource/3b01bcb8-0b14-4abf-b6f2-c1bfd384ba69?api-key={}&format=json&offset={}&limit=10".format(api_key,offno)
response = requests.get(url)
data = response.text
a1=json.loads(data)
for ele in a1['records']:
if ele['pollutant_min'] == 'NA':
dict1['pol_min'] = 0
else:
dict1['pol_min'] = int(ele['pollutant_min'])
if ele['pollutant_max'] == 'NA':
dict1['pol_max'] = 0
else:
dict1['pol_max'] = int(ele['pollutant_max'])
if ele['pollutant_avg'] == 'NA':
dict1['pol_avg'] = 0
else:
dict1['pol_avg'] = int(ele['pollutant_avg'])
dict1['state'] = ele['state']
dict1['city'] = ele['city']
dict1['station'] = ele['station']
dict1['time_vector'] = ele['last_update']
lst2.append(dict1.copy())
if a1["count"] < 10:
a= False
offno += 10
airx = json.dumps(lst2, indent=1)
tdata.objects.bulk_create([tdata(**vals) for vals in lst2])
return airx
答案 0 :(得分:0)
你有两种方法
答案 1 :(得分:0)
Celery 可能比你需要的更复杂,比如他每小时运行一次。您已经发现了编写管理命令。只需将管理命令包装在一个 shell 脚本中,然后(在 Unix/Linux 上)让 cron 每小时运行一次。您需要确保 shell 脚本在成功时保持“安静”,但要使任何失败都非常明显,因此它不会坐在那里失败而没有人注意到。
不能建议在 Windows 上做什么,但我认为它有任务调度。
答案 2 :(得分:0)
你可以试试simple-scheduler,因为它很简单!这是给你的一个小例子。
from time import sleep, ctime, time
from simple_scheduler.recurring import recurring_scheduler
def wait_t_secs(t):
began_at = ctime(time())
sleep(t)
print(f"I waited {t} seconds. [From: {began_at} to {ctime(time())}]")
recurring_scheduler.add_job(target=wait_10_secs,
period_in_seconds=5,
job_name="ten",
kwargs={"t":10})
recurring_scheduler.job_summary()
recurring_scheduler.run()
recurring_scheduler.job_summary()