Question

i have a little script wich is taking the number of tweets using tweetstream and write that to a file, but i see there is a limit of 50/s, maybe because the file IO, i want to try to store the value in a variable and then, every 10 minutes write the file with the value of the variable, how can i do that?

#!/usr/bin/python
import tweetstream
import sys
print sys.argv
twitterUsername = "username"
twitterPassword = "password"
twitterWordFilter = sys.argv[1]

try:
    with tweetstream.FilterStream(twitterUsername, twitterPassword,track=twitterWordFilter) as stream:
        for tweet in stream:
            file = open('/monitor/'+str(sys.argv[2])+'.txt','w+')
            file.write(str(stream.count))
            file.close
            #print tweet #Use for raw output

except tweetstream.ConnectionError, e:
    print "Disconnected from twitter. Reason:", e.reason
Was it helpful?

Solution

The comment from @tdelaney is correct.

Time it first! File writes are already cached by python (well, clib really) and the operating system before hitting the disk. Its more likely the twitter stream is slow. You could get the time right before and after the write and print the diff. BTW, you also want cowboy legs on the close file.close(). And since you are just opening the same file over and over, just open it once outside of the for loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top