Created
April 10, 2021 03:46
-
-
Save Justintime50/3a415006006bffdeb3a78fa81b7856b4 to your computer and use it in GitHub Desktop.
An example of how to limit concurrent threads in Python
This file contains 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 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