如何编写一个多线程程序进行交互式绘图? [英] How to write a multi-threaded program for plotting interactively?
问题描述
我尝试使用以下代码每1秒从网站上在线阅读数据,然后实时绘制结果。我的意思是我喜欢在之前的情节中每秒添加新的last_price并更新它。
I am trying to read data online from a website every 1 seconds using following code, then plot the result in real time. I mean I like to add new last_price in every second to the previous plot and update it.
import time
import requests
for i in range(2600):
time.sleep(1)
with requests.Session() as s:
data = {'ContractCode' : 'SAFTR98'}
r = s.post('http://cdn.ime.co.ir/Services/Fut_Live_Loc_Service.asmx/GetContractInfo', json = data ).json()
for key, value in r.items():
print(r[key]['ContractCode'])
last_prices = (r[key]['LastTradedPrice'])
我使用了animation.FuncAnimation但是没有用,因为它绘制了2600次迭代后的所有结果完成或我停止程序!所以我想也许我可以用多线程来完成工作。但我不知道我该怎么用?我搜索过但无法理解这些例子,我应该如何将它映射到我的问题?
这不是一个重复的问题。在上面提到的链接中,我尝试使用Animations解决问题,但在这个问题中,我试图找到一种使用多线程的新方法。
我是什么尝试过:
我试图使用动画功能,但没有用。你可以在这里查看:https://stackoverflow.com/questions/56087708/how-to-plotting-online-with-pyplot
I used animation.FuncAnimation but didn't work because it plots all the results after the 2600 iterations is done or I stop the program! So I thought maybe I can do the work with multi-threading. But I don't know how exactly should I use it? I searched about but couldn't understand the examples and how should I map it to my problem?
This is not a duplicated question. In the mentioned link I tried to solve the problem using Animations but in this question I am trying to find a new way using multi-threading.
What I have tried:
I tried to use animation function but didn't work. You can check it out here:https://stackoverflow.com/questions/56087708/how-to-plotting-online-with-pyplot
推荐答案
计时器 - 什么是在Python中每x秒重复执行一次函数的最佳方法? - 堆栈溢出 [ ^ ]
这篇关于如何编写一个多线程程序进行交互式绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!