该线程以无的形式生成输出,并在Python中生成相同的结果

2024-03-28 21:08:14 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码和收到的输出:
代码

from iqoptionapi.stable_api import IQ_Option
import time
import threading
import pandas as pd
goal="EURUSD"
email = "email"
password = "password"
Candles_Required = 3 
ANS=[]
print("login...")
I_want_money=IQ_Option(email,password)
I_want_money.suspend = 0.1
Threads = []
times = [1565257105, 1565197105, 1565137105]
print("All Times Series : ",times)
def candles_history(tt):
    global goal,ANS,times
    print(times[tt])
    data=I_want_money.get_candles(goal, 60, 1000, times[tt])
    ANS =data+ANS
    print("Executed Time : ",tt)
    df = pd.DataFrame(data)
    df.to_csv("AllTimes//"+str(times[tt])+".txt",   index=False)
    return len(data)

for i in range(Candles_Required):
   t = threading.Thread(target = candles_history,args=(i,) ) 
   Threads.append(t) 
   t.start()
   print(i," Process Started")  
for thread in Threads:    
    while thread.is_alive():
        print("Thread_output : ",thread.join())
        print("Thread is alive")
        pass
print("Len ANS : ", len(ANS))
df=pd.DataFrame(ANS)  
df = df.sort_values('to')  
print(df.head(5))

上述脚本的输出

login...
All Times Series :  [1565257105, 1565197105, 1565137105]
1565257105
0  Process Started
1565197105
1  Process Started
1565137105
2  Process Started
Executed Time :  1
Executed Time :  0
Executed Time :  2
Thread_output :  None
Thread is alive
Thread_output :  None
Thread is alive
Thread_output :  None
Thread is alive
Len ANS :  3000
                       at    close        from      id       max      min      open          to  volume
0     1565197200001474593  1.12271  1565197140  660039  1.122795  1.12266  1.122755  1565197200     185
1000  1565197200001474593  1.12271  1565197140  660039  1.122795  1.12266  1.122755  1565197200     185
2000  1565197200001474593  1.12271  1565197140  660039  1.122795  1.12266  1.122755  1565197200     185
1     1565197260022881266  1.12296  1565197200  660040  1.122960  1.12271  1.122720  1565197260     321
1001  1565197260022881266  1.12296  1565197200  660040  1.122960  1.12271  1.122720  1565197260     321

结果表明:
我运行thread.join之后的输出是None
即使是print(df.head(5))的输出,前三个值也是相同的。
我试图使用API从Iqoption加载3000支蜡烛:IQOPTION_API
但我得到的输出是3000支蜡烛,但都是一样的。我猜不出这些线是怎么回事

3000支蜡烛需要3秒钟,但我想在1秒内得到结果。因此,我们尝试了线程

请告诉我怎样才能在一秒钟内得到结果。
我希望这一点我是清楚的。如果有人需要更多信息,请在我将解释的评论中告诉我


Tags: importdfdatatimeisprocessthreadprint