, run a Thread , Thread . , boolean, . , ; , , :
import logging, time, threading, requests
logging.basicConfig(format='%(asctime)s %(levelname)s : %(message)s', level=logging.INFO)
class EppThread(threading.Thread):
kill = False
url = 'https://v1.api.com/example'
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, verbose=None):
threading.Thread.__init__(self, group=group, target=target, name=name, verbose=verbose)
self.args = args
def run(self):
limit = self.args[0]
for i in range(limit):
response = requests.get(self.url)
if response.status_code != 200:
self.kill = True
else:
print('Thread {0} - success'.format(self.getName()))
if self.kill:
break
print('process completed')
number_of_threads = 5
number_of_requests = 100
if __name__ == '__main__':
startTime = time.time()
threads = []
for i in range(number_of_threads):
et = EppThread(name="{0}-Thread-{1}".format(name, i + 1), args=(number_of_requests))
threads.append(et)
et.start()
while threads and time.time() - startTime < 60:
time.sleep(0.5)
for thread in threads:
if not thread.isAlive():
threads.remove(thread)
print('Thread {0} terminated'.format(thread.getName()))
EppThread.kill = True
, - EppThreads , True, EppThread . EppThread.kill = True , , 1 .
, url. , URL- , . , , - EppThread kill url.
class EppThread2(EppThread):
kill = False
url = 'https://v2.example.com/api?$awesome=True'
EppThread2 threads, , .