多线程加锁并发任务

# 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()

  

posted @ 2019-03-31 15:28  不带R的墨菲特  阅读(304)  评论(0编辑  收藏  举报