Skip to content

Instantly share code, notes, and snippets.

@cloverstd

cloverstd/thread.py

Created Dec 26, 2019
Embed
What would you like to do?
多线程
import requests
from Queue import Queue, Empty
import time
import threading
wait_q = Queue()
lock = threading.Lock()
success = list()
failed = list()
url = None
class Worker(threading.Thread):
def run(self):
while not wait_q.empty():
try:
item = wait_q.get(False)
except Empty:
continue
if not item:
continue
resp = requests.get(url)
result = resp.json()
lock.acquire()
success.append(result)
lock.release()
if __name__ == '__main__':
wait_q.put('1')
workers = [Worker() for i in xrange(500)]
for worker in workers:
worker.start()
for worker in workers:
worker.join()
print '*' * 10, 'success'
data = list()
for i in success:
data.append('{},{}'.format(i[0], i[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.