Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am making a utility in which I need to extract tweets for a user and store them in database. Twitter API only sends 20 tweets in one call and to retrieve more tweets, you need to page through the tweets 20 at a time. So instead of synchronously reading tweets from Twitter API and waiting to insert into DB, I want to start the database inserts asynchronously to optimize the process.
How can I achieve this with Python and Mysql?
Pseudo-code for this can be written as (not checked for syntax validity):-



    def readTweets():
       x=0
       while true: 
          tweets= twitterAPI.getusertimeline(id='twitterUser',count=20,page=x)
          #Need to know how to call the below function asynchronously
          callDBSaveAsynchronously(tweets)
          if len(tweets) < 20: break
          x=x+1

    def callDBSaveAsynchronously(tweets):
        for tweet in tweets:
            mysqldb.insertTweet(tweet)

 

Thanks in advance!

share|improve this question
    
With Postgres, use twitter_fdw to directly SELECT * FROM twitter WHERE from_user='twitterUser'; –  Mike T Jun 4 '13 at 23:21
    
you could relegate each set of tweets or tweet page url you want insert to a job queue, and have a pool of workers to insert those tweets rabbitmq.com/tutorials/tutorial-two-python.html –  dm03514 Jun 5 '13 at 0:12
    
Thanks, RabbitMQ makes lots of sense.... Do you know if python objects can be passed as messages to the queue? –  whosthr Jun 6 '13 at 16:15

1 Answer 1

There's a third-party MySQL driver called ultramysql that can take advantage of gevent for async inserts. You could avoid threads that way. It worked great for me on a recent project.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.