在Python中同时对URL发出多个POST请求

2024-06-30 07:26:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Django和Django Rest框架。在我的urls.py中,我定义了以下端点/payments/。它支持POST请求。你知道吗

背景信息:不久前,我们有一个用户向这个服务器发送多个请求,同时触发一个竞争条件,因此偷钱。你知道吗

问题:

  1. 如何编写一个测试来向这个URL API端点发送100-1000个请求?你知道吗

这是我当前在测试文件中发送POST“测试”请求的方式:

class PaymentViewTestCase(BaseTestCase):
    def setUp(self):
        super(PaymentViewTestCase, self).setUp()
        self.client = APIClient()
        self.client.force_authenticate(user=self.profile)

    def test_post_create_payment(self):
        amount = 1000
        request_data = {
            'amount': amount,
        }
        res = self.client.post(
            '/payment/',
            ujson.dumps(request_data),
            content_type='application/json',
            secure=True
        )

但是,我想同时触发这个POST请求1000。你知道吗


Tags: djangoselfclient框架restdatarequestdef
1条回答
网友
1楼 · 发布于 2024-06-30 07:26:47
from concurrent.futures import ProcessPoolExecutor
import os

def task():
    print("Executing our Task on Process {}".format(os.getpid()))

def main():
    executor = ProcessPoolExecutor(max_workers=3)
    task1 = executor.submit(task)
    task2 = executor.submit(task)

if __name__ == '__main__':
    main()

相关问题 更多 >