Skip to content

Instantly share code, notes, and snippets.

@benhoyt
Created November 3, 2016 13:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhoyt/c35aaa46935bda0180ce9a2b75a4db0d to your computer and use it in GitHub Desktop.
Save benhoyt/c35aaa46935bda0180ce9a2b75a4db0d to your computer and use it in GitHub Desktop.
Test how many threads we can run at once
"""Test how many threads we can run at once."""
import itertools
import threading
import time
import sys
import requests
successes = itertools.count()
failures = itertools.count()
def thread_func():
while True:
time.sleep(0.5)
try:
requests.get('http://www.example.com').text
next(successes)
except:
next(failures)
def main(n):
threading.stack_size(64 * 1024)
for i in range(n):
thread = threading.Thread(target=thread_func)
thread.start()
time.sleep(0.005)
n = 0
while True:
time.sleep(1)
print(next(successes) - n, next(failures) - n)
n += 1
if __name__ == '__main__':
main(int(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment