• 多线程模块:threading


    threading 常见用法:

    (1) 在 thread 中,我们是通过 thread.start_new_thread(function, args) 来开启一个线程,接收一个函数和该函数的参数,函数的参数必须是元组的形式
    (2) 在 threading 中,我们是通过 threading.Thread(target=function, args=(....)) 来创建一个对象,然后再通过对象的 start() 方法来开启一个线程,多个线程可以并发执行

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    import time
    import threading
    
    def fun():
        print time.ctime()
        time.sleep(1)
    
    for i in range(3):
        t = threading.Thread(target=fun, args=())    # 创建一个 Thread 对象,接收一个函数和该函数的参数
        t.start()                                    # 通过对象来开启一个新的线程,通过线程来执行函数
    [root@localhost ~]$ python 1.py    # 多个线程并发执行,只需要执行一秒
    Mon Jan 28 21:15:35 2019
    Mon Jan 28 21:15:35 2019
    Mon Jan 28 21:15:35 2019

    threading 有两种方法可以开启一个新的线程:

    (1) 如上面的例子,通过创建 Thread 对象,然后通过对象的 start() 方法来运行一个新的线程
    (2) 第二种方法是继承 threading.Thread 这个类, 然后重写 run() 方法来运行一个新的线程

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    import time
    import threading
    
    class MyThread(threading.Thread):
    
        def __init__(self):
            threading.Thread.__init__(self)
    
        def run(self):
            print time.ctime()
            time.sleep(1)
    
    for i in range(3):
        t = MyThread()
        t.start()
    [root@localhost ~]$ python 1.py 
    Mon Jan 28 23:53:02 2019
    Mon Jan 28 23:53:02 2019
    Mon Jan 28 23:53:02 2019


    threading.Lock():用于创建一个锁对象,我们可以同时开启多个线程,但是在任意时刻只能有一个线程能在解释器运行,因此需要由全局解锁器(GIL)控制运行哪个线程

    锁对象的常用方法如下:

        lock = threading.Lock() 创建一个锁对象
        lock.acquire() 锁上锁,当某个线程被锁上之后,会优先执行这个锁的内容,直到锁释放才会执行其他线程
        lock.locked() 查看锁的状态,如果已经被锁上了则返回True,否则返回False
        lock.release() 释放锁,必须先获得锁才能释放锁

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    import time
    import threading
    
    def hello():
        for i in range(3):
            hello_lock.acquire()
            print('hello'),
            time.sleep(1)
            world_lock.release()
    
    def world():
        for i in range(3):
            world_lock.acquire()
            print('world')
            time.sleep(1)
            hello_lock.release()
    
    
    hello_lock = threading.Lock()
    world_lock = threading.Lock()
    
    world_lock.acquire()
    t1 = threading.Thread(target=hello, args=())
    t2 = threading.Thread(target=world, args=())
    
    t1.start()
    t2.start()
    [root@localhost ~]$ python 1.py 
    hello world
    hello world
    hello world

    其他常用方法:

    t.join()方法用于阻塞下一个线程的运行,等待当前线程执行完再执行下一个线程,例子如下:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    import time
    import threading
    
    def fun():
        print time.ctime()
        time.sleep(1)
    
    for i in range(3):
        t = threading.Thread(target=fun, args=())
        t.start()
        t.join()
    [root@localhost ~]$ python 1.py     # 由于线程被阻塞,需要执行3秒
    Mon Jan 28 21:33:35 2019
    Mon Jan 28 21:33:36 2019
    Mon Jan 28 21:33:37 2019


    t.setDaemon()方法用于设置当前线程随着主线程的退出而退出,例子如下:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    import time
    import threading
    
    def fun():                    # 函数的功能是打印三次当前时间
        for i in range(3):   
    print time.ctime() time.sleep(1) t = threading.Thread(target=fun, args=()) t.setDaemon(True) t.start()

    print 'End...' # 执行 print 之后,主线程就退出了,最终结果只会打印一次
    [root@localhost ~]$ python 1.py 
    Mon Jan 28 21:54:48 2019
    End...

     
    t.getName() 用于获取线程的名字,t.setName() 用于重新设置线程的名字,默认的线程名是 Thread-1,Thread-2,Thread-3,......

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    import time
    import threading
    
    def fun():
        newName = 'new-' + t.getName()
        print newName
        time.sleep(1)
    
    for i in range(3):
        t = threading.Thread(target=fun, args=())
        t.start()
    [root@localhost ~]$ python 1.py 
    new-Thread-1
    new-Thread-2
    new-Thread-3

        

         

  • 相关阅读:
    二维数组转换
    二维数组转换
    二分法原理
    二维数组举例
    二分法原理
    二维数组举例
    二分法原理
    二分法原理
    linux中BIND服务程序安全的加密传输TSIG机制
    linux系统中部署DNS从服务器
  • 原文地址:https://www.cnblogs.com/pzk7788/p/10355006.html
Copyright © 2020-2023  润新知