Last active
September 23, 2019 10:57
-
-
Save tawateer/573c804fe190e188c1185c81559282fa to your computer and use it in GitHub Desktop.
python multi thread example
This file contains hidden or 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
#!/bin/env python | |
# -*- coding:utf-8 -*- | |
import threading | |
sem = threading.Semaphore(10) | |
result = [] | |
def run_thread(n): | |
with sem: | |
global result | |
result.append(2*n) | |
# print len(threading.enumerate()) | |
def main(): | |
threads = [] | |
n = 1000 | |
for i in xrange(n): | |
t = threading.Thread(target=run_thread, args=(i,)) | |
threads.append(t) | |
for i in xrange(n): | |
threads[i].start() | |
for i in xrange(n): | |
threads[i].join() | |
print result | |
print len(result) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment