• 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


    
                                        
    
  • 相关阅读:
    第十一课:容器监控和Prometheus介绍
    第五课:单机编排利器:Docker Compose
    第四课:企业级镜像仓库Harbor
    第三课:快速部署LNMP平台
    负载均衡
    中间系统到中间系统IS-IS
    ansble 常识
    centos 的两种密码破解方式
    git在windos下使用
    git 本地仓库和远程仓库搭建
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349540.html
Copyright © 2020-2023  润新知