【问题标题】:Making several twitter API post requests quickly快速发出多个 twitter API 发布请求
【发布时间】: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 个直接消息帖子,所以我认为这不是我的问题。感谢您的回复

标签: python twitter


【解决方案1】:

看看http://docs.python.org/dev/library/concurrent.futures.html#threadpoolexecutor-example

concurrent.futures 可作为 pypi 上旧 Python 版本的反向移植

【讨论】:

  • 是的,我之前在看这个。也许我遗漏了一些东西,但这个问题以及 Grequests 模块都存在的问题是我看不到如何让它向一个 url 发送多个请求。这两个模块提供的所有示例都只显示了它们在向几个不同的 url 发送请求时的用处。
  • 只需使用 URLS = [yoururl] * 50
  • 让它工作,但似乎这个模块只有助于处理多个相同的帖子请求。不幸的是,它并没有提高多次引用同一帖子网址的速度:(
  • 它可以做你需要做的事情。它只是关于线程。由于您的示例并不完整(此处也没有帖子),因此很难提供更多帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多