• 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()
  • 相关阅读:
    统一身份认证部署ca 问题
    用组件beanutils,dbutils简化JDBC操作
    java 几种远程服务调用协议的比较
    让Tomcat支持路径、中文文件名(浏览器的get方式) .
    Spring 2.5中JdbcTemplate类query方法的三种回调接口
    tomcat 支持文件下载
    Eclipse 插件开发遇到问题心得总结
    eclipse user library的创建
    SCSI>SAS磁盘与FC磁盘的比较说明
    关于properties文件的字符编码
  • 原文地址:https://www.cnblogs.com/xiuxiu123456/p/10898722.html
Copyright © 2020-2023  润新知