python 多线程另外一种写法

来源:互联网 发布:poi导入excel数据库 编辑:程序博客网 时间:2024/06/03 06:18
import threadingfrom time import sleep,ctimeimport cx_Oracleimport timeloops=[9,5]def exp1():     conn = cx_Oracle.connect('test/test@192.168.137.2/serv')     cursor = conn.cursor()     owner = "system"     #writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)     tname = threading.current_thread()     print tname     exportOracleSql = "select 'exp01' from dual"     print exportOracleSql     x = cursor.execute(exportOracleSql)     time.sleep(10)     cursor.close()     conn.close()def exp2():    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')    cursor = conn.cursor()    owner = "system"    # writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)    tname = threading.current_thread()    print tname    exportOracleSql = "select 'exp02' from dual"    print exportOracleSql    x = cursor.execute(exportOracleSql)    time.sleep(10)    cursor.close()    conn.close()def exp3():    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')    cursor = conn.cursor()    owner = "system"    # writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)    tname = threading.current_thread()    print tname    exportOracleSql = "select 'exp03' from dual"    print exportOracleSql    x = cursor.execute(exportOracleSql)    time.sleep(10)    cursor.close()    conn.close()def exp4():    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')    cursor = conn.cursor()    owner = "system"    # writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)    tname = threading.current_thread()    print tname    exportOracleSql = "select 'exp04' from dual"    print exportOracleSql    x = cursor.execute(exportOracleSql)    time.sleep(10)    cursor.close()    conn.close()def exp5():    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')    cursor = conn.cursor()    owner = "system"    # writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)    tname = threading.current_thread()    print tname    exportOracleSql = "select 'exp05' from dual"    print exportOracleSql    x = cursor.execute(exportOracleSql)    time.sleep(10)    cursor.close()    conn.close()def exp6():    conn = cx_Oracle.connect('test/test@192.168.137.2/serv')    cursor = conn.cursor()    owner = "system"    # writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)    tname = threading.current_thread()    print tname    exportOracleSql = "select 'exp06' from dual"    print exportOracleSql    x = cursor.execute(exportOracleSql)    time.sleep(10)    cursor.close()    conn.close()def main():    print 'starting at:',ctime()    threads=[]    nloops=[exp1,exp2,exp3,exp4,exp5,exp6]    abs=range(len(nloops))    j=0    for i in nloops:        a = "%s%s" % ('exp_thread', i)        t=threading.Thread(target=i,name=a)        threads.append(t)        j = j + 1    print threads    for i in abs:        print threading.current_thread()        threads[i].start()    for i in abs:        threads[i].join()        print threading.current_thread()    print 'all Done at:',ctime()if __name__=='__main__':    main()C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p12.pystarting at: Thu Sep 14 08:28:18 2017[<Thread(exp_thread<function exp1 at 0x025ACE30>, initial)>, <Thread(exp_thread<function exp2 at 0x0262B5F0>, initial)>, <Thread(exp_thread<function exp3 at 0x0262B630>, initial)>, <Thread(exp_thread<function exp4 at 0x0262B670>, initial)>, <Thread(exp_thread<function exp5 at 0x0262B6B0>, initial)>, <Thread(exp_thread<function exp6 at 0x0262B6F0>, initial)>]<_MainThread(MainThread, started 9852)><_MainThread(MainThread, started 9852)><_MainThread(MainThread, started 9852)><_MainThread(MainThread, started 9852)><_MainThread(MainThread, started 9852)><_MainThread(MainThread, started 9852)><Thread(exp_thread<function exp1 at 0x025ACE30>, started 9340)>select 'exp01' from dual<Thread(exp_thread<function exp2 at 0x0262B5F0>, started 8780)>select 'exp02' from dual<Thread(exp_thread<function exp6 at 0x0262B6F0>, started 8940)>select 'exp06' from dual<Thread(exp_thread<function exp4 at 0x0262B670>, started 9488)>select 'exp04' from dual<Thread(exp_thread<function exp3 at 0x0262B630>, started 8420)>select 'exp03' from dual<Thread(exp_thread<function exp5 at 0x0262B6B0>, started 4684)>select 'exp05' from dual<_MainThread(MainThread, started 9852)><_MainThread(MainThread, started 9852)><_MainThread(MainThread, started 9852)><_MainThread(MainThread, started 9852)><_MainThread(MainThread, started 9852)><_MainThread(MainThread, started 9852)>all Done at: Thu Sep 14 08:28:28 2017Process finished with exit code 0


 
原创粉丝点击