user3276768
user3276768

Reputation: 1436

Get real time data over http in Python

I have an small issue and I'm wondering what's an optimal way to do this. So, I have a web service/API/web link that is updated every X amount of time (not a specific time) and I'm trying to write a small program that gets that data each time the content is updated.

My current program access the web link each 2 seconds to retrieve the data, however since this service is not always updated every 2 seconds, I am loosing data.

Can anyone suggest a more efficient way to do this? I'm not sure if this is possible (or easy to do), but i was thinking about keeping an open connection and read the data each time the service is updated (is this even possible?). Thanks.

This is a sample of what I have (mostly pseudocode).

def getdata():
    threading.Timer(2.0, getdata).start()
    response = urllib2.urlopen('http://www.example.com/data.json')
    html = response.read()
    print 'read'

Upvotes: 0

Views: 899

Answers (1)

icktoofay
icktoofay

Reputation: 129109

There is no magical generic way to be notified when some data is updated. The service you are using would need to have explicit support for that; for example, Twitter has an endpoint commonly called “the firehose” that streams the public timeline to you. Before that was available, there was no alternative to polling, and so if your service has no analogue, you will need to continue to poll.

Upvotes: 2

Related Questions