gpt4 book ai didi

python-3.x - 在 Python 3 中每 x 秒发出一次 API 请求

转载 作者:行者123 更新时间:2023-12-04 12:45:32 24 4
gpt4 key购买 nike

我正在尝试使用 Python 3 在服务器上进行压力测试。这个想法是每 1 秒向 API 服务器发送一个 HTTP 请求,持续 30 分钟。我尝试使用 requestsapscheduler这样做,但我一直得到

Execution of job "send_request (trigger: interval[0:00:01], next run at: 2017-05-23 11:05:46 EDT)" skipped: maximum number of running instances reached (1)



我怎样才能使这项工作?以下是我到目前为止的代码:
import requests, json, time, ipdb
from apscheduler.schedulers.blocking import BlockingScheduler as scheduler

def send_request():
url = 'http://api/url/'

# Username and password
credentials = { 'username': 'username', 'password': 'password'}

# Header
headers = { 'Content-Type': 'application/json', 'Client-Id': 'some string'}

# Defining payloads
payload = dict()

payload['item1'] = 1234
payload['item2'] = 'some string'
data_array = [{"id": "id1", "data": "some value"}]
payload['json_data_array'] = [{ "time": int(time.time()), "data": data_array]

# Posting data
try:
request = requests.post(url, headers = headers, data = json.dumps(payload))
except (requests.Timeout, requests.ConnectionError, requests.HTTPError) as err:
print("Error while trying to POST pid data")
print(err)
finally:
request.close()

print(request.content)

return request.content

if __name__ == '__main__':
sched = scheduler()
print(time.time())
sched.add_job(send_request, 'interval', seconds=1)
sched.start()
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

try:
# This is here to simulate application activity (which keeps the main thread alive).
while true:
pass
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()

我尝试搜索堆栈溢出,但到目前为止,其他问题都没有做我想要的,或者我错过了一些东西。如果是这种情况,我将不胜感激有人指出我正确的线程。非常感谢!

最佳答案

我认为我标记的重复以及@jeff 的答案很好地描述了您的错误

编辑:显然不是..所以在这里我将描述如何解决最大实例问题:

最大实例数问题

当您向调度程序添加作业时,您可以为作业的最大允许并发实例数设置一个参数。您可以 应该在这里阅读:
BaseScheduler.add_job()

因此,解决您的问题只是将其设置为更高的问题:

sch.add_job(myfn, 'interval', seconds=1, max_instances=10)

但是,您想要多少并发请求?如果他们的响应时间超过一秒,而你每秒请求一个,如果让它运行足够长的时间,你最终总会得到一个错误......

调度器

有几个调度程序选项可用,这里有两个:

后台调度器

您正在导入阻塞调度程序 - 它在启动时阻塞。因此,直到调度程序停止后才会执行其余代码。如果您需要在启动调度程序后执行其他代码,我会像这样使用后台调度程序:
from apscheduler.schedulers.background import BackgroundScheduler as scheduler

def myfn():
# Insert your requests code here
print('Hello')

sch = scheduler()
sch.add_job(myfn, 'interval', seconds=5)
sch.start()

# This code will be executed after the sceduler has started
try:
print('Scheduler started, ctrl-c to exit!')
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input()" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep(.1)

#pass
#input()
except KeyboardInterrupt:
if sch.state:
sch.shutdown()

阻塞调度器

如果启动调度器后不需要执行其他代码,可以使用阻塞调度器,更简单:
apscheduler.schedulers.blocking import BlockingScheduler as scheduler

def myfn():
# Insert your requests code here
print('Hello')

# Execute your code before starting the scheduler
print('Starting scheduler, ctrl-c to exit!')

sch = scheduler()
sch.add_job(myfn, 'interval', seconds=5)
sch.start()

关于python-3.x - 在 Python 3 中每 x 秒发出一次 API 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44138848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com