码迷,mamicode.com
首页 > 其他好文 > 详细

100w并发产生唯一随机id

时间:2019-05-23 00:17:53      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:lda   exe   form   port   random   data   随机id   UNC   example   

#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:\n\t-c : concurrent TotalData\n\t -f: filename‘s path\n--example:"         "\n\tpython 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),"\nmake {} datas time cost {}".format(concurrent_data,during),‘\n‘)
if __name__ == ‘__main__‘:
    enterParams()

  

100w并发产生唯一随机id

标签:lda   exe   form   port   random   data   随机id   UNC   example   

原文地址:https://www.cnblogs.com/SunshineKimi/p/10909249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!