Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Justintime50
Created April 10, 2021 03:46
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 Justintime50/3a415006006bffdeb3a78fa81b7856b4 to your computer and use it in GitHub Desktop.
Save Justintime50/3a415006006bffdeb3a78fa81b7856b4 to your computer and use it in GitHub Desktop.
An example of how to limit concurrent threads in Python
import time
from threading import BoundedSemaphore, Thread
def main():
max_num_threads = 100
thread_limiter = BoundedSemaphore(max_num_threads)
# OS's have limits on the number of threads that can be opened at once,
# be aware of that with this number (eg: don't try something like 10,000+)
# Using this method will spawn all threads at once but only run the number
# of threads specified in `max_num_threads` at a given time
for i in range(1000):
Thread(
target=print_string,
args=(
thread_limiter,
i,
)
).start()
def print_string(thread_limiter, i):
try:
thread_limiter.acquire()
print('Hello World!' + str(i))
time.sleep(1)
finally:
thread_limiter.release()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment