Created
February 22, 2021 06:46
-
-
Save leadscloud/e31bb661d6760220cd29ff2479224c20 to your computer and use it in GitHub Desktop.
python thread多线程
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
thread_num = 0 | |
lock = threading.Lock() | |
def do_request(): | |
global thread_num | |
# ------------- | |
# my code here | |
# ------------- | |
with lock: | |
thread_num -= 1 | |
while True: | |
if thread_num <= 50: | |
with lock: | |
thread_num += 1 | |
t = threading.Thread(target=do_request) | |
t.start() | |
else: | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment