zoukankan      html  css  js  c++  java
  • 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()
    

      

  • 相关阅读:
    HDOJ 1000 A + B Problem C++解法
    HDU之旅
    C++primer plus第六版课后编程题答案8.8
    C++primer plus第六版课后编程题答案8.6
    C++primer plus第六版课后编程题答案8.5
    C++primer plus第六版课后编程题答案8.3(正解)
    C语言练手自己编写学生成绩管理系统
    C_数据结构
    C---数组名作函数参数
    C---通过指针访问数组
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/10909249.html
Copyright © 2011-2022 走看看