zoukankan      html  css  js  c++  java
  • python 多线程另外一种写法

    import threading
    from time import sleep,ctime
    import cx_Oracle
    import time
    loops=[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="
    ", 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="
    ", 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="
    ", 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="
    ", 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="
    ", 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="
    ", 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:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p12.py
    starting 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 2017
    
    Process finished with exit code 0


    
                                        
    
  • 相关阅读:
    window screen (获取屏幕信息)
    setTimeout 与 setInterval
    php 安装程序
    字符串string 对象发的应用
    数组 array 的方法使用
    angular js h5关于表单验证的例子
    angularjs 路由的应用
    勾选复选框按钮可用否则不可用
    h5正则验证表单
    常见的正则表达式
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349540.html
Copyright © 2011-2022 走看看