使用多个线程调用函数时,报告TimeoutError 10060

时间:2019-05-16 14:29:11

标签: python multithreading request urllib3

我正在尝试从nse网站检索一些库存信息,并使用nsepy库。现在,如果我尝试通过使用线程发出并发请求,则会报告10600错误。如果我仅添加1秒钟的睡眠,代码似乎可以正常工作。知道为什么需要1秒钟的延迟。

from nsetools import Nse  
from nsepy import get_history  
from datetime import date  
import talib as ta  
import threading  
import time


def get_and_cal_RSI(stock_code):  
    stock_details = get_history(symbol=stock_code, start=date(2017,1,1),
                                end=date(2019,5,15)) 
    print("calculating rsi for " + stock_code)  
    close_arr = stock_details.get("Close")  
    rsi= ta.RSI(close_arr, 14)  
    rsi_value = rsi.get(date(2019,5,15))  
    if rsi_value is not None and rsi_value > 50 :  
        print(stock_code)


nse = Nse()  
all_stocks= nse.get_stock_codes()

for code, stock_name in all_stocks.items():  
    if not (code == 'SYMBOL'):  
        threading.Thread(target=get_and_cal_RSI, args=(code,)).start()    
        time.sleep(1) // adding this line makes the code work fine but its 
                         abit slow and without the sleep the following error is reported:
requests.exceptions.ConnectionError: ('Connection aborted.', TimeoutError(10060, 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond', None, 10060, None))

0 个答案:

没有答案