Closed gogog22510 closed 1 year ago
This shouldn't happen because database access is managed by a in-memory mutex, should be thread-safe. Are you running multiple Docker instances with yfinance
running concurrently?
I'm running one Docker instance with multiple threads, the command in the Docker image is from the Flask official guide, something like gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 'flaskr:create_app()'
My theory is since I'm running multiple threads, the yfinance
api potentially will be called in multiple requests, and l saw the mutex is part of the cache instance (https://github.com/ranaroussi/yfinance/blob/96ff2141079d8627c6b137ee802bea80e9f1229b/yfinance/utils.py#L701), and the cache instance is lazy initialized here (https://github.com/ranaroussi/yfinance/blob/96ff2141079d8627c6b137ee802bea80e9f1229b/yfinance/base.py#L780).
Therefore, if the first time yfinance
api is called by multiple threads, potentially, the cache instance will initialize twice, and the database lock error may happen. (Currently, I only saw this error when I newly start my Docker instance and send request from my frontend)
Good theory. Fix is simple, do you mind adding and testing yourself? In _TzCache::__init__()
, append at bottom: self.tz_db
.
The fix makes sense, let me try it on my side.
I am still getting this error on newer versions ( for e.g. 0.2.18
). This is what I tried :
In [2]: import yfinance as yf
In [3]: df = yf.download('MSFT', start='2023-01-01', end='2023-01-31')
[*********************100%***********************] 1 of 1 completed
1 Failed download:
- MSFT: IntegrityError('NOT NULL constraint failed: kv.key')
However, if I try the above using version 0.1.96
, it works.
Latest version on GitHub should fix this, try it and feedback. Instructions: #1080
Thanks, it works in the latest version. ( I tried it using the instructions )
Great, will send out a release soon. Just for completeness, the fix is #1504.
Actually, you should be able to revert to the normal PIP version now. Because that error only appears during a one-off migration process. Feed back.
The PIP version works now. Thanks @ValueRaider !
data = yf.download( # or pdr.get_data_yahoo(...
tickers list or string as well, cannot be an numpy.ndarray
)