开发者社区> 问答> 正文

关于多线程的一个问题·求助? 报错

关于多线程的一个问题·求助? 400 报错

环境:win2003+mysql5+python2.7


# -*- coding: cp936 -*-

import thread
import MySQLdb

def init():
    global DATABASE_NAME
    DATABASE_NAME = 'chengji'
    global HOST
    HOST = 'localhost'
    global PORT
    PORT = '3306'
    global USER_NAME
    USER_NAME = 'root'
    global PASSWORD
    PASSWORD = 'root'
    global CHAR_SET
    CHAR_SET = 'utf8'
      
#获取数据库连接
def get_conn():
    init()
    return MySQLdb.connect(host = HOST, user = USER_NAME, passwd = PASSWORD, db = DATABASE_NAME, charset = CHAR_SET)
  
#获取cursor
def get_cursor(conn):
    return conn.cursor()
  
#关闭连接
def conn_close(conn):
    if conn != None:
        conn.close()
  
#关闭cursor
def cursor_close(cursor):
    if cursor != None:
        cursor.close()
  
#关闭所有
def close(cursor, conn):
    cursor_close(cursor)
    conn_close(conn)
	

#数据库中表情况
def show_tables():
    sql = 'show tables'
    conn = get_conn()
    cursor = get_cursor(conn)
    result = cursor.execute(sql)
    for row in cursor.fetchall():
        print row[0]
        #table_name = row[0]


def query_data(table):

        #threadLock.acquire()
          
        sql = 'select  * from '+ table +' where fenshu = "90"'
        conn = get_conn()
        cursor = get_cursor(conn)
        result = cursor.execute(sql)
        for row1 in cursor.fetchall():
          #姓名,性别,学号,分数
          print row1[1],row1[2],row1[3],row1[4]
        close(cursor, conn)
        
        #threadLock.release()

       
#threadLock=thread.allocate_lock()


#查询表信息

#def query_chaxun1():
i=0
sql = 'show tables'
conn = get_conn()
cursor = get_cursor(conn)
result = cursor.execute(sql)
for row in cursor.fetchall():
  print row[0]
  # 有多少个表就创建多少新线程(语文 数学 英语 政治 历史 地理 物理 化学 体育)
  i=i+1
  print i

  thread.start_new_thread(query_data(row[0])),(i,)





上面的thread.start_new_thread创建线程出错,thread.start_new_thread带的函数不能带参数吗?能的话要怎么写?

展开
收起
优选2 2020-06-04 17:27:54 505 0
1 条回答
写回答
取消 提交回答
  • from threading import Thread 
    from time import sleep 
    def threaded_function(arg,szText): 
        for i in range(arg): 
            szTest = szText + " running\n" 
            print szTest 
            sleep(1) 
    
    if __name__ == "__main__": 
        thread1 = Thread(target = threaded_function, args = (10, 'thread1')) 
        thread2 = Thread(target = threaded_function, args = (10, 'thread2')) 
        thread1.start() 
        thread2.start() 
        thread2.join() 
        thread1.join() 
        print "thread finished...exiting"
    
    #参考:http://stackoverflow.com/questions/2905965/creating-threads-in-python




    2020-06-05 13:37:48
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
多IO线程优化版 立即下载
Android插件化-从入门到"放弃" 立即下载
低代码开发师(初级)实战教程 立即下载