码迷,mamicode.com
首页 > 其他好文 > 详细

并发编程(四十六)

时间:2019-03-08 18:25:16      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:main   alt   需要   roc   算法   end   closed   lis   process   

http://www.cnblogs.com/linhaifeng/articles/6817679.html

进程是对正在运行程序的一个抽象。即使可以利用的cpu只有一个(早期的计算机确实如此),也能保证支持(伪)并发的能力。

将一个单独的cpu变成多个虚拟的cpu(多道技术:时间多路复用和空间多路复用+硬件上支持隔离),没有进程的抽象,现代计算机将不复存在。

#一 操作系统的作用:
    1:隐藏丑陋复杂的硬件接口,提供良好的抽象接口
    2:管理、调度进程,并且将多个进程对硬件的竞争变得有序

#二 多道技术:
    1.产生背景:针对单核,实现并发
    ps:
    现在的主机一般是多核,那么每个核都会利用多道技术
    有4个cpu,运行于cpu1的某个程序遇到io阻塞,会等到io结束再重新调度,会被调度到4个
    cpu中的任意一个,具体由操作系统调度算法决定。
    
    2.空间上的复用:如内存中同时有多道程序
    3.时间上的复用:复用一个cpu的时间片
       强调:遇到io切,占用cpu时间过长也切,核心在于切之前将进程的状态保存下来,这样
            才能保证下次切换回来时,能基于上次切走的位置继续运行


线程

import threading
import time
def listen(parm):
    print("%s start listening %s... " %(time.ctime(),parm))
    time.sleep(3)
    print("%s end listening %s" %(time.ctime(),parm))

def play():
    print("%s start play...." %(time.ctime()))
    time.sleep(5)
    print("%s end play..." %(time.ctime()))


if __name__ == "__main__":
    t1 = threading.Thread(target=listen, args=("music",))
    t2 = threading.Thread(target=play)

    t1.start()
    t2.start()

    print("%s end of main..." %time.ctime())

‘‘‘
Fri Mar  8 17:14:31 2019 start listening music... 
Fri Mar  8 17:14:31 2019 start play....Fri Mar  8 17:14:31 2019 end of main...

Fri Mar  8 17:14:34 2019 end listening music
Fri Mar  8 17:14:36 2019 end play...
‘‘‘

join()方法

主线程要等待join的线程结束之后才能退出

技术图片
import threading
import time
def listen(parm):
    print("%s start listening %s... " %(time.ctime(),parm))
    time.sleep(3)
    print("%s end listening %s" %(time.ctime(),parm))

def play():
    print("%s start play...." %(time.ctime()))
    time.sleep(5)
    print("%s end play..." %(time.ctime()))


if __name__ == "__main__":
    t1 = threading.Thread(target=listen, args=("music",))
    t2 = threading.Thread(target=play)

    t1.start()
    t2.start()

    t1.join()
    t2.join()

    print("%s end of main..." %time.ctime())

‘‘‘
Fri Mar  8 17:18:01 2019 start listening music... 
Fri Mar  8 17:18:01 2019 start play....
Fri Mar  8 17:18:04 2019 end listening music
Fri Mar  8 17:18:06 2019 end play...
Fri Mar  8 17:18:06 2019 end of main...
‘‘‘
View Code
技术图片
import threading
import time
def listen(parm):
    print("%s start listening %s... " %(time.ctime(),parm))
    time.sleep(3)
    print("%s end listening %s" %(time.ctime(),parm))

def play():
    print("%s start play...." %(time.ctime()))
    time.sleep(5)
    print("%s end play..." %(time.ctime()))


if __name__ == "__main__":
    t1 = threading.Thread(target=listen, args=("music",))
    t2 = threading.Thread(target=play)

    t1.start()
    t2.start()

    t1.join()
    #t2.join()

    print("%s end of main..." %time.ctime())

‘‘‘
Fri Mar  8 17:20:32 2019 start listening music... 
Fri Mar  8 17:20:32 2019 start play....
Fri Mar  8 17:20:35 2019 end listening music
Fri Mar  8 17:20:35 2019 end of main...
Fri Mar  8 17:20:37 2019 end play...
‘‘‘
View Code

 

守护线程Daemon()

必须在线程start()之前设置,守护线程与主线程同时结束

技术图片
import threading
import time
def listen(parm):
    print("%s start listening %s... " %(time.ctime(),parm))
    time.sleep(3)
    print("%s end listening %s" %(time.ctime(),parm))

def play():
    print("%s start play...." %(time.ctime()))
    time.sleep(5)
    print("%s end play..." %(time.ctime()))


if __name__ == "__main__":
    t1 = threading.Thread(target=listen, args=("music",))
    t2 = threading.Thread(target=play)

    t2.setDaemon(True)

    t1.start()
    t2.start()

    print("%s end of main..." %time.ctime())

‘‘‘
Fri Mar  8 17:22:34 2019 start listening music... 
Fri Mar  8 17:22:34 2019 start play....Fri Mar  8 17:22:34 2019 end of main...

Fri Mar  8 17:22:37 2019 end listening music
‘‘‘
View Code

无论是进程还是线程,都遵循:守护xxx会等待主xxx运行完毕后被销毁

需要强调的是:运行完毕并非终止运行

#1.对主进程来说,运行完毕指的是主进程代码运行完毕

#2.对主线程来说,运行完毕指的是主线程所在的进程内所有非守护线程统统运行完毕,主线程才算运行完毕
#1 主进程在其代码结束后就已经算运行完毕了(守护进程在此时就被回收),然后主进程会一直等非守护的子进程都运行完毕后回收子进程的资源(否则会产生僵尸进程),才会结束,

#2 主线程在其他非守护线程运行完毕后才算运行完毕(守护线程在此时就被回收)。因为主线程的结束意味着进程的结束,进程整体的资源都将被回收,而进程必须保证非守护线程都运行完毕后才能结束。

迷惑人的例子
注意下面两个例子的时间

技术图片
from threading import Thread
import time
def foo():
    print(123)
    time.sleep(1)
    print("end123")

def bar():
    print(456)
    time.sleep(3)
    print("end456")


t1=Thread(target=foo)
t2=Thread(target=bar)

t1.daemon=True
t1.start()
t2.start()
print("main-------")
‘‘‘
123
456main-------

end123
end456
‘‘‘
View Code
技术图片
from threading import Thread
import time
def foo():
    print(123)
    time.sleep(4)
    print("end123")

def bar():
    print(456)
    time.sleep(3)
    print("end456")


t1=Thread(target=foo)
t2=Thread(target=bar)

t1.daemon=True
t1.start()
t2.start()
print("main-------")
‘‘‘
123
456main-------

end456

Process finished with exit code 0
‘‘‘
View Code

 

threading.activeCount(),
getName()
getName()
技术图片
import threading
import time
def listen(parm):
    print("%s start listening %s... " %(time.ctime(),parm))
    time.sleep(3)
    print("%s end listening %s" %(time.ctime(),parm))

def play():
    print("%s start play...." %(time.ctime()))
    time.sleep(5)
    print("%s end play..." %(time.ctime()))


if __name__ == "__main__":
    t1 = threading.Thread(target=listen, args=("music",))
    t2 = threading.Thread(target=play)

    t2.setDaemon(True)

    t1.start()
    t2.start()

    print(threading.activeCount()) # 3
    print(t1.getName()) # Thread-1
    print(t2.getName()) # Thread-1
    
    t1.setName("hello")
    print(t1.getName()) # hello

    print("%s end of main..." %time.ctime())
View Code

 

并发编程(四十六)

标签:main   alt   需要   roc   算法   end   closed   lis   process   

原文地址:https://www.cnblogs.com/xiangtingshen/p/10497461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!