python 中的 http post 请求非常慢
Very slow http post requests in python
我创建了一个 python 脚本,它从 facebook 图 api 收集 json 数据并检查用户 job_title 信息。
通过使用此脚本,我通知用户通过聊天机器人更新他们的 job_title,但此过程需要太多时间才能向所有用户发送请求。
import json
import requests
users_url = Facebook API to fetch user details
MESSAGE_TO_SEND = '....PLEASE UPDATE JOB TITLE....'
ACCESS_TOKEN = Page_Access_token
def reply(user_id, msg,ACCESS_TOKEN):
data = {
"recipient": { "id": user_id },
"message": { "text": msg }
}
resp = requests.post("https://graph.facebook.com/v9.0/me/messages?access_token="+ ACCESS_TOKEN, json=data)
print('Message Sent to : ',user_id)
# print(resp.content, resp, 'response from facebook')
def header(ACCESS_TOKEN):
return {'Authorization': 'Bearer ' + ACCESS_TOKEN}
def user_data(ACCESS_TOKEN):
headers = header(ACCESS_TOKEN)
data = requests.get(users_url,headers=headers)
result_json = json.loads(data.text)
resources = result_json['Resources']
for titles in range(0,len(resources)):
if 'title' not in resources[titles]:
user_id = str(resources[titles]['id'])
reply(user_id, MESSAGE_TO_SEND,ACCESS_TOKEN)
user_data(ACCESS_TOKEN)
请帮助我....我能做什么?
调整示例here...
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import as_completed
import time
def square(n):
time.sleep(3.0)
print( n * n )
def main():
values = range(10)
with ThreadPoolExecutor(max_workers = 5) as executor:
results = executor.map(square, values)
# for result in results:
# print(result)
if __name__ == '__main__':
st = time.time()
main()
et = time.time()
print('{:.3f} seconds'.format(et-st))
将 values
替换为您的 user_ids 列表,将 square
替换为您的 reply
函数并将 max_workers
设置为您喜欢的数字。
我创建了一个 python 脚本,它从 facebook 图 api 收集 json 数据并检查用户 job_title 信息。
通过使用此脚本,我通知用户通过聊天机器人更新他们的 job_title,但此过程需要太多时间才能向所有用户发送请求。
import json
import requests
users_url = Facebook API to fetch user details
MESSAGE_TO_SEND = '....PLEASE UPDATE JOB TITLE....'
ACCESS_TOKEN = Page_Access_token
def reply(user_id, msg,ACCESS_TOKEN):
data = {
"recipient": { "id": user_id },
"message": { "text": msg }
}
resp = requests.post("https://graph.facebook.com/v9.0/me/messages?access_token="+ ACCESS_TOKEN, json=data)
print('Message Sent to : ',user_id)
# print(resp.content, resp, 'response from facebook')
def header(ACCESS_TOKEN):
return {'Authorization': 'Bearer ' + ACCESS_TOKEN}
def user_data(ACCESS_TOKEN):
headers = header(ACCESS_TOKEN)
data = requests.get(users_url,headers=headers)
result_json = json.loads(data.text)
resources = result_json['Resources']
for titles in range(0,len(resources)):
if 'title' not in resources[titles]:
user_id = str(resources[titles]['id'])
reply(user_id, MESSAGE_TO_SEND,ACCESS_TOKEN)
user_data(ACCESS_TOKEN)
请帮助我....我能做什么?
调整示例here...
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import as_completed
import time
def square(n):
time.sleep(3.0)
print( n * n )
def main():
values = range(10)
with ThreadPoolExecutor(max_workers = 5) as executor:
results = executor.map(square, values)
# for result in results:
# print(result)
if __name__ == '__main__':
st = time.time()
main()
et = time.time()
print('{:.3f} seconds'.format(et-st))
将 values
替换为您的 user_ids 列表,将 square
替换为您的 reply
函数并将 max_workers
设置为您喜欢的数字。