Python3 多线程threading

记录的是自己工作中的简单例子,希望能讲解清楚多线程的使用

import threading
import requests
import json


# 多线程测试
def show():
    url = "请求的URL"

    payload = json.dumps({
        "service": "login"  # 参数
    })
    headers = {
       'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)


for i in range(15):
    t = threading.Thread(target=show, args=())  # 如果启动的线程函数有入参,则在args中输入,注意传入的参数一定是一个元组!例如args=(1,2,)
  t.start()
  # 如果需要等待所有线程结束才退出,就加上 t.jion()

 

posted @ 2022-07-15 16:47  骨灰盒少女  阅读(132)  评论(0编辑  收藏  举报