• Python多线程(生成动态参数)


    Python多线程方法如下:
    import threading
    import datetime
    import pymssql
    import time
    from faker import Faker
    
    
    def insert_db():
        for i in range(100):
            for j in range(100):
                print(j)
                time1 = datetime.datetime.now()
                fake = Faker(locale='zh_CN')
                name = fake.name()
                ssn = fake.ssn()
                card_number = fake.credit_card_number()
                phone_number = fake.phone_number()
                print(datetime.datetime.now() - time1)
                print(name, ssn, card_number, phone_number)
                conn = pymssql.connect(host='ip', user='sa', password='password', database='db',
                                       charset='UTF-8')
                cur = conn.cursor()
                sql = 'This is the sql'
                print(sql)
    
                cur.execute(sql)
                conn.commit()
    
    
            cur.close()
            conn.close()
    
    
    exitFlag = 0
    
    
    class myThread(threading.Thread):
        def __init__(self, threadID, name, counter):
            threading.Thread.__init__(self)
            self.threadID = threadID
            self.name = name
            self.counter = counter
    
        def run(self):
            print("开始线程:" + self.name)
            print_time(self.name, self.counter, 5)
            insert_db()
            print("退出线程:" + self.name)
    
    
    def print_time(threadName, delay, counter):
        while counter:
            if exitFlag:
                threadName.exit()
            time.sleep(delay)
            print("%s: %s" % (threadName, time.ctime(time.time())))
            counter -= 1
    
    
    for i in range(50):
        # 创建新线程
        locals()['thread' + str(i)] = myThread(i, "Thread-%s" % i, i)
    
    for i in range(50):
        # 开启新线程
        locals()['thread' + str(i)].start()
    
    for i in range(50):
        locals()['thread' + str(i)].join()
    
    print("退出主线程")

    locals()['thread' + str(i)]
    这个方法可以生成thread1,thread2,thread3...这些参数
    Faker 是python造数常用的一个库
     
  • 相关阅读:
    题解 POJ1149 Pigs
    题解 【网络流24题】运输问题
    题解 【网络流24题】太空飞行计划
    题解 【网络流24题】方格取数问题
    题解 密码锁
    题解 【重庆八中模拟赛】寻找代表元
    题解 [SHOI2010]最小生成树
    题解 【ZJOI2009】 假期的宿舍
    题解 [ZJOI2008]树的统计Count
    JSP页面中的pageEncoding和contentType两种属性(转)
  • 原文地址:https://www.cnblogs.com/carlvine/p/13343726.html
Copyright © 2020-2023  润新知