0
点赞
收藏
分享

微信扫一扫

多线程加锁并发任务

唯米天空 2022-09-20 阅读 62

# coding=utf-8
import threading
import time
import string
import random


class TestTreading(threading.Thread):
def __init__(self,func):
threading.Thread.__init__(self)
self.func =func
#@override
def run(self):
mutex = threading.Lock()
mutexFlage = mutex.acquire()
if mutexFlage:
self.res=self.func()
print(time.asctime())
mutex.release()
return self.res
# def get_res(self):
# return self.res
def add():
return ''.join(random.sample(string.hexdigits,random.randint(16,18)))

def executors():
resList=[]
pool=[]
for i in range(10000):
th=TestTreading(add)
pool.append(th)

for t in pool:
t.start()
for t in pool:
t.join()
resList.append(t.run())
return resList
if __name__ == '__main__':
executors()

  



举报

相关推荐

0 条评论