How to handle concurrent requests in Python multi-threading?

In Python, you can use multiple threads to achieve concurrent requests. Below is a simple example code demonstrating how to use multiple threads to send concurrent requests.

import threading
import requests

# 定义一个函数,用于发送请求
def make_request(url):
    response = requests.get(url)
    print(response.text)

# 定义要发送的请求列表
urls = ['http://example.com', 'http://example.org', 'http://example.net']

# 创建一个线程列表
threads = []

# 创建并启动线程
for url in urls:
    thread = threading.Thread(target=make_request, args=(url,))
    thread.start()
    threads.append(thread)

# 等待所有线程执行完毕
for thread in threads:
    thread.join()

In the code above, we first define a function called make_request for sending requests. Then, we create a list of requests urls that contains the URLs of the requests to be sent. Next, we create a list of threads threads to store the created threads. Then, we use the threading.Thread class to create threads, specifying the function to be executed using the target parameter, and passing the function’s parameters using the args parameter. We then call the start method to start the threads and add them to the thread list. Finally, we use the join method to wait for all threads to finish executing.

It is important to ensure thread safety when sending requests using multiple threads. You can utilize thread locks (threading.Lock) to ensure that multiple threads do not access shared resources simultaneously.

bannerAds