【发布时间】:2014-01-23 10:50:32
【问题描述】:
我一直在尝试编写一个应用程序,该应用程序将通过 python REQUEST 模块发送几个 twitter api 发布请求,其中时间至关重要。但是,当我开始发送更多此类请求时,处理时间会呈指数级增长。
我已经梳理了其他几篇相关的帖子,但找不到适合这个问题的帖子。我已经看到了一些使用异步或线程的建议,但由于 oauth 的要求,我在实现它时相当不成功。
我的代码截图如下:
import urllib2
import json
from requests_oauthlib import OAuth1
import httplib2
import requests
from datetime import datetime
print "found?"
found = raw_input()
url = 'https://api.twitter.com/1.1/direct_messages/new.json'
def get_oauth():
oauth = OAuth1("xxxxxxxxx",
client_secret="xxxxxxxxxxxx",
resource_owner_key="xxxxxxxxxx",
resource_owner_secret="xxxxxxxxxxxxxxx")
return oauth
def get_oauthfranco():
oauthfranco = OAuth1("xxxxxxxxxxxxxx",
client_secret="xxxxxxxxxxxx",
resource_owner_key="xxxxxxxxxxxx",
resource_owner_secret="xxxxxxxx")
return oauthfranco
startTime = datetime.now()
oauth = get_oauth()
oauthf = get_oauthfranco()
requests.post(url, data={'screen_name':'vuoc','text':'hello ppls ' + found}, auth=oauth, stream=True)
requests.post(url, data={'screen_name':'vuoc','text':'hello ppls number 2 ' + found}, auth=oauth)
requests.post(url, data={'screen_name':'vuoc','text':'hello ppls number 3 ' + found}, auth=oauthf, stream=True)
requests.post(url, data={'screen_name':'vuoc','text':'hello ppls number 4 ' + found}, auth=oauthf)
requests.post(url, data={'screen_name':'vuoc','text':'hello ppls number 5 ' + found}, auth=oauths, stream=True)
当然,最终产品不仅仅包含 4 个帖子请求(希望做 20 多个)。
非常感谢任何关于如何通过线程/异步优化速度同时仍然能够使用 oauth 的建议!
【问题讨论】:
-
也许不是你的代码问题?你知道节流 (dev.twitter.com/docs/rate-limiting/1.1) 吗?
-
我看到它限制每个应用程序一次超过 15 个请求。我在不同的应用程序之间分配了我的请求,并且从未一次测试过超过 12 个直接消息帖子,所以我认为这不是我的问题。感谢您的回复