dpguthrie / yahooquery

Python wrapper for an unofficial Yahoo Finance API
https://yahooquery.dpguthrie.com
MIT License
767 stars 137 forks source link

unclosed ssl.SSLSocket #72

Open odufb opened 3 years ago

odufb commented 3 years ago

hi all, I am new at python programming. I just try to get data using history function and there is no problem if I dont use thread. If I try to use thread I get following errors.

"ResourceWarning: unclosed <ssl.SSLSocket fd=5084, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, .....>"

"Exception ignored in: <ssl.SSLSocket fd=5264, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, ...."

these errors/warnings dont effect my program but it is annoying to see a lot of unwanted text at console.

I try to get rid of these warnings by using warnings.simplefilter("ignore", ResourceWarning) statement but it is useless.

I am not sure is this a important issue but any help about this topic appreciated

dpguthrie commented 3 years ago

What version of python are you using?

odufb commented 3 years ago

3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]

kunif commented 1 year ago

This seems to be caused by the self.session initialized in the constructor not being close() when the instance of _YahooFinance in base.py is destroyed.

For example, instead of retrieving multiple tickers in a list at once, it occurs when you create a method that processes them one by one in a loop.

def get_info_of_specified_ticker(ticker):
    result_data = {}
    tickerinfo = Ticker(ticker)
    summary = tickerinfo.summary_detail[ticker]
    # Process to extract only desired information to result_data
    return result_data #### At this point the warning in the title occurs

What the user can do is to explicitly close() the session before the instance is destroyed.

    # Process to extract only desired information to result_data
    tickerinfo.session.close() #### Explicitly close() the session
    return result_data

Essentially, add a destructor to the _YahooFinance class in base.py and close() self.session in it. If you have other resources to deal with in your destructor, you should add those as well.