Skip to content

Instantly share code, notes, and snippets.

@tawateer
Last active September 23, 2019 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tawateer/573c804fe190e188c1185c81559282fa to your computer and use it in GitHub Desktop.
Save tawateer/573c804fe190e188c1185c81559282fa to your computer and use it in GitHub Desktop.
python multi thread example
#!/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