• python3.6_多线程和多进程


    1.多线程

    #多线程实例
    from time import sleep,ctime
    import threading
    
    #多个函数
    def talk(content,loop):
        for x in range(loop):
            print('Start talk:%s  %s' %(content,ctime()))
            sleep(3)
    
    def write(content,loop):
        for x in range(loop):
            print('Start write:%s  %s' %(content,ctime()))
            sleep(5)
    
    #定义多个线程
    threads=[]
    t1=threading.Thread(target=talk,args=('线程1开始',2))
    threads.append(t1)
    
    t2=threading.Thread(target=write,args=('线程2开始',2))
    threads.append(t2)
    
    #运行线程 
    if __name__=='__main__':
        for x in threads:
            x.start()
        for x in threads:
            x.join() #线程守护,保证每个线程都运行完成
    
        print('over %s' %ctime())

    2.多线程锁

    Python中有两种锁,一个锁是原始的锁(原语), 不可重入,而另一种锁则是可重入的锁即递归锁。而是thread模块中,只提供了不可重入的锁,而在threading中则提供这两种锁。

    可重入:当一个线程拥有一个锁的使用权后,再次获取锁的使用权时,不会阻塞,会立马得到使用权,则原始锁的话,则不行,会阻塞。

    方法一、thread不可重入锁

    import thread
    
    import time
    
     
    
    lock = thread.allocate_lock()
    
     
    
    def Count(id):
    
        global num;
    
     
    
        while True:
    
            lock.acquire()
    
            if num <= 10:
    
                print "Thread id is : %s     The num is %s
    " % (id, str(num))
    
                num = num + 1
    
            else:
    
                break
    
            lock.release()
    
        else:
    
            thread.exit_thread()
    
     
    
    if __name__ == "__main__":
    
        num = 1
    
        thread.start_new_thread(Count, ('A',))
    
        thread.start_new_thread(Count, ('B',))
    
     
    
        time.sleep(5)

    方法二、theading的Lock(不可重入锁)

    import threading
    
    import time
    
     
    
    lock = threading.Lock()
    
     
    
    def Count(id):
    
        global num;
    
     
    
        while True:
    
            lock.acquire()
    
            if num <= 10:
    
     
    
                print "Thread id is : %s     The num is %s
    " % (id, str(num))
    
                num = num + 1
    
            else:
    
                break
    
            lock.release()
    
     
    
    if __name__ == "__main__":
    
        num = 1
    
        t1 = threading.Thread(target=Count, args=('A', ))
    
        t2 = threading.Thread(target=Count, args=('B', ))
    
     
    
        t1.start()
    
        t2.start()
    
     
    
        time.sleep(5)

    方法三:threading的RLock(可重入)

    import threading
    
    import time
    
     
    
    lock = threading.RLock()
    
     
    
    def CountNum(id):
    
        global num
    
         
    
        lock.acquire()
    
         
    
        if num <= 10:
    
            print "Thread id is : %s     The num is %s
    " % (id, str(num))
    
            num = num + 1
    
            CountNum(id)
    
     
    
        lock.release()
    
     
    
    if __name__ == "__main__":
    
        num = 1
    
        t1 = threading.Thread(target=CountNum, args=('A'))
    
     
    
        t1.start()
    
     
    
        time.sleep(5)

    3、多进程实例

    #多进程实例
    from time import sleep,ctime
    from multiprocessing import Process
    
    #多个函数
    def talk(content,loop):
        for x in range(loop):
            print('Start talk:%s  %s' %(content,ctime()))
            sleep(2)
    
    def write(content,loop):
        for x in range(loop):
            print('Start write:%s  %s' %(content,ctime()))
            sleep(3)
    
    #定义多个线程
    processes=[]
    p1=Process(target=talk,args=('进程1开始',2))
    processes.append(p1)
    
    p2=Process(target=write,args=('进程2开始',2))
    processes.append(p2)
    
    #运行线程 
    if __name__=='__main__':
        for x in processes:
            x.start()
        for x in processes:
            x.join() #线程守护,保证每个线程都运行完成
    
        print('over %s' %ctime())

    4.多进程锁

    from multiprocessing import Process,Lock
    # 线程的锁是为了防止共享数据产生错误,从而加锁保重每次操作数据只有一个线程
    # 进程的锁是为了在共享屏幕时不会出错,比如打印时不会打乱了
    
    def f(l,i):
        l.acquire()
        try:
            print('hello world',i)
        finally:
            l.release()
    
    if __name__ == '__main__':
        lock = Lock()
        for num in range(10):
            Process(target=f,args=(lock,num)).start()
  • 相关阅读:
    软件工程导论P53,习题2.4
    视图和数据表的区别
    无法从“object”转换为“string”
    Oracle 密码重置
    Struts2 上传下载
    Spring 事务管理
    JSP 指令和动作
    JS 禁用回车、后退事件、form 表单不可编辑
    关于 in 和 exist 的理解
    Oracle clob 操作函数
  • 原文地址:https://www.cnblogs.com/xiuxiu123456/p/10898722.html
Copyright © 2020-2023  润新知