• 100w并发产生唯一随机id


    #coding=utf-8
    import time
    import base64
    import getopt
    import sys
    import threading
    import random
    import string
    import os
    def base64_cal():
        str = 'admin'
        str = str.encode('utf-8')
        # 加密
        bs64 = base64.b64encode(str)
        # 解密
        debs64 = base64.b64decode(bs64)
        print(debs64.decode("utf-8"))
        # base32bit加密
        bs32 = base64.b32encode(str)
        debs32 = base64.b32decode(bs32)
        print(debs32.decode("utf-8"))
    
    def enterParams():
        opts, args = getopt.getopt(sys.argv[1:], '-h-c:f:', ['help', 'concurrent=', 'file='])
        concurrent_data=None
        filename=None
        for opt_name, opt_value in opts:
            print(opt_name,opt_value)
            if opt_name in ('-h', '--help'):
                usage()
                sys.exit()
            if opt_name in ('-c', '--concurrent'):
                if type(opt_value).__name__=='str':
                    concurrent_data=opt_value
            if opt_name in ('-f', '--file'):
                    filename = opt_value
                # else:
                #     # return None
                #     raise TypeError("enter param {} not file's path".format(opt_value))
    
            return concurrent_data,filename
    
    def usage():
        str="--help:
    	-c : concurrent TotalData
    	 -f: filename's path
    --example:" 
            "
    	python testfile.py -c 1000000 -file=./res/testPython.txt"
        print(str)
    
    class ConcurrentIdSample(threading.Thread):
        def __init__(self,func,args=()):
            self.func=func
            self.args=args
            threading.Thread.__init__(self)
        def run(self):
            self.result = self.func(*self.args)
        def get_result(self):
            try:
                return self.result
            except Exception:
                return None
    
    def func(*args):
        # print(time.asctime())
        return ''.join(random.sample(string.hexdigits,random.randint(*args)))
    
    def concurrent_execute(concurrent_data):
        pool=[]
        samplesList=[]
        for i in range(concurrent_data):
            thread_sample=ConcurrentIdSample(func,(16,20))
            pool.append(thread_sample)
            thread_sample.start()
        for thread in pool:
            thread.join()
            samplesList.append(thread.get_result())
        return samplesList
    
    
    def outPut():
        st=time.time()
        concurrent_data,filename=enterParams()
        res=concurrent_execute(concurrent_data)
        end=time.time()
        during=end-st
        print(len(res),"
    make {} datas time cost {}".format(concurrent_data,during),'
    ')
    if __name__ == '__main__':
        enterParams()
    

      

  • 相关阅读:
    eWebEditor漏洞
    远程连接SQL Server
    eWebEditor漏洞
    【Demo 0009】绘图对象调色板
    【Demo 0001】 获取命令行参数
    【Demo 0007】绘图对象 画笔
    【Demo 0005】Win32 程序结构封装
    【Demo 0006】图形绘制基础
    【Demo 0008】绘图对象–画刷
    【Demo 0004】Windows 消息机制(2)
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/10909249.html
Copyright © 2020-2023  润新知