import pymysql
import threading
defsql_insert():
conn = pymysql.connect('localhost','root',"shiyi",'dailyfresh')
cus= conn.cursor()#id = int(id)try:for i inrange(1000):
sql=("INSERT INTO aaa VALUES (0,'%s')")%i
#tlock.acquire()
cus.execute(sql)
conn.commit()# print(ok)#tlock.release()except Exception as e:print("one error happen",e)finally:pass
t1=threading.Thread(target=sql_insert)
t1.start()
targer=指定的函数名在Thread类的def run()方法中调用,源码:
defrun(self):"""Method representing the thread's activity.
You may override this method in a subclass. The standard run() method
invokes the callable object passed to the object's constructor as the
target argument, if any, with sequential and keyword arguments taken
from the args and kwargs arguments, respectively.
"""try:if self._target:
self._target(*self._args,**self._kwargs)#开始调用target指定函数名的函数finally:# Avoid a refcycle if the thread is running a function with# an argument that has a member that points to the thread.del self._target, self._args, self._kwargs