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造数常用的一个库