• 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造数常用的一个库
     
  • 相关阅读:
    airtest支持Cocos-Creator,SDK接入
    Python的getter和setter方法
    liunx常用命令
    liunx的vim常用命令收集
    django+atx+liunx的一些shell脚本
    django+airtest+atx部署liunx平台
    Android下的鉴权实现方案
    Mate20 pro实现H265 (HEVC)实时硬件编码
    OpenCV C++常用功能介绍
    Ubuntu16.04编译libjpeg-turbo库
  • 原文地址:https://www.cnblogs.com/carlvine/p/13343726.html
Copyright © 2020-2023  润新知