简体   繁体   中英

Django CoinMarketCap API is not updating prices(django.db.utils.OperationalError: database is locked)

I have a website that displays cryptocurrency prices with an SQLite db but unfortunately the prices have stopped updating for some reason. I get an django.db.utils.OperationalError: database is locked error when I run coin_update.py I tried adding

'OPTIONS': {
            'timeout': 20,
        }

to my settings.py but the error presists

This is what my coin_update.py file looks like:

import os, django, sys
os.environ['DJANGO_SETTINGS_MODULE']='website.settings'
django.setup()
import datetime
print("Cron Script for coinmarketcap API initialized")
from requests import Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
from cryptocracy.models import Coin, CoinHistory
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
parameters = {
    'start': '1',
    'limit': '5000',
    'convert': 'USD',
}
headers = {
    'Accepts': 'application/json',
    'X-CMC_PRO_API_KEY': 'aa817498-b4b3-48da-9774-eb54c26d846b',
}
session = Session()
session.headers.update(headers)
try:
    response = session.get(url, params=parameters)
    data = json.loads(response.text)
    coin_data=data
except (ConnectionError, Timeout, TooManyRedirects) as e:
    print(e)
    coin_data=e
integrity_error_count=0
api_length=len(coin_data['data'])
for coin in coin_data['data']:
    update_coin=Coin.objects.filter(ticker=coin['symbol'])
    if update_coin:
        is_coin=True
        if coin['quote']['USD']['percent_change_24h']:
            change24h=coin['quote']['USD']['percent_change_24h']
        else:
            change24h=0
        if coin['quote']['USD']['volume_24h']:
            volume=coin['quote']['USD']['volume_24h']
        else:
            volume=0
        if coin['quote']['USD']['price']:
            price=coin['quote']['USD']['price']
        else:
            price=0
        if coin['circulating_supply']:
            supply=coin['circulating_supply']
        else:
            supply=0
        if coin['max_supply']:
            max_supply=coin['max_supply']
        else:
            max_supply=0
        if coin['total_supply']:
            total_supply=coin['total_supply']
        else:
            total_supply=0
        if coin['quote']['USD']['market_cap']:
            market_cap=coin['quote']['USD']['market_cap']
        else:
            market_cap=0
        if coin['platform']:
            is_coin=False
        save_history=update_coin[0]
        CoinHistory.objects.create(coin=save_history, price=save_history.price, supply=save_history.supply, volume=save_history.volume, total_supply=save_history.total_supply, max_supply=save_history.max_supply, market_cap=save_history.market_cap, change24h=save_history.change24h)
        update_coin.update(price=price, supply=supply, volume=volume,total_supply=total_supply, max_supply=max_supply, market_cap=market_cap, change24h=change24h)
    else:
        integrity_error_count=integrity_error_count+1
        continue
print("There are " + str(integrity_error_count) + " coins from search that did not match..")
print("Cron Script for coinmarketcap API ended")

I have not been able to find similar issues on stackoverflow so any help would be appreciated.

I think this is because of sqlite database. I once ran into a similar issue and it turns out it was not able to handle the load. I have since moved on to postgres and l haven't encounter that error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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