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

      

  • 相关阅读:
    【linux】Centos下登陆mysql报错#1045
    tomcat在centos7里面启动很慢的解决办法
    tomcat日志文件 转载https://www.cnblogs.com/operationhome/p/9680040.html
    tomcat的文件目录结构
    centos 7服务器下tomcat 问题 1.配置问题
    x11转发遇到的问题
    x11转发,可以在shell里面看到图形界面
    linux里面tomcat配置遇到的问题
    vim中文乱码 vim字符集设置
    c#.net常见字符串处理方法
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/10909249.html
Copyright © 2020-2023  润新知