线程

自定义线程简单示例代码:

import threading

class MyThread(threading.Thread):
    def __init__(self,num):
        super(MyThread,self).__init__()
        self.num = num

    def run(self):
        print(self.num)

mt = MyThread(3)
mt.start()

 

守护线程,和查看线程id主线程结束后不管子线程有没有运行完,子线程都全部被结束掉:

执行python  xxx.py时,会生成一个进程,这个进程会自动“启用一个线程”,如果在程序中启用线程,或由启用的线程再去启动子线程时(待补充,需查找获取线程id的方法)

import threading
import time
import os

def m(i):
    time.sleep(i)
    print("m os.getpid()=",i,os.getpid(),os.getppid())

def f(n):
    t_list = []
    for i in range(n):
        t = threading.Thread(target=m,args=(i,))
        t.start()
        print("f t.getName() = %s, os.getpid() = %s,os.getppid()= %s"%(t.getName(),os.getpid(),os.getppid()))
        t_list.append(t)
    for t in t_list:
        t.join()

if __name__ == "__main__":

    t = threading.Thread(target=f,args=(10,))
    t.setDaemon(True)
    t.start()
    print("main t.getName() = %s, os.getpid() = %s,os.getppid()= %s"%(t.getName(),os.getpid(),os.getppid()))
    time.sleep(6)
    print("main end")

运行结果,可以看到进程Id只有一个,父进程id为pycharm, 父线程为守护线程,父线程结束后,自线程也不再运行。

main t.getName() = Thread-1, os.getpid() = 452,os.getppid()= 7644
f t.getName() = Thread-2, os.getpid() = 452,os.getppid()= 7644
m os.getpid()= 0 452 7644
f t.getName() = Thread-3, os.getpid() = 452,os.getppid()= 7644
f t.getName() = Thread-4, os.getpid() = 452,os.getppid()= 7644
f t.getName() = Thread-5, os.getpid() = 452,os.getppid()= 7644
f t.getName() = Thread-6, os.getpid() = 452,os.getppid()= 7644
f t.getName() = Thread-7, os.getpid() = 452,os.getppid()= 7644
f t.getName() = Thread-8, os.getpid() = 452,os.getppid()= 7644
f t.getName() = Thread-9, os.getpid() = 452,os.getppid()= 7644
f t.getName() = Thread-10, os.getpid() = 452,os.getppid()= 7644
f t.getName() = Thread-11, os.getpid() = 452,os.getppid()= 7644
m os.getpid()= 1 452 7644
m os.getpid()= 2 452 7644
m os.getpid()= 3 452 7644
m os.getpid()= 4 452 7644
m os.getpid()= 5 452 7644
main end

C:\WINDOWS\system32>tasklist|find "7644"
pycharm.exe 7644 Console 1 756,268 K

C:\WINDOWS\system32>tasklist|find "452"
360chrome.exe 3068 Console 1 20,452 K
python.exe 452 Console 1 10,200 K

 

Event.

event = threading.Event()

event.set()     # 设置event

event.wait()  #event.isSet()为False时阻塞

event.clear() #清除event.set()

event.isSet() #判断event是否设置

 

信号量,同时只允许指定量的线程运行。

 

import time,threading

def a(i):
    s.acquire()
    print("begin: ",i,time.time())
    time.sleep(i)
    print("end:   ",i,time.time())
    s.release()

if __name__ == "__main__":
    s = threading.BoundedSemaphore(5)
    t_list = []
    for i in range(10):
        t = threading.Thread(target=a,args=(i,))
        t.start()
        t_list.append(t)
    for t in t_list:
        t.join()

运行结果:

 1 begin:  0 1466304270.705245
 2 end:    0 1466304270.705245
 3 begin:  1 1466304270.7061803
 4 begin:  2 1466304270.7061803
 5 begin:  3 1466304270.7061803
 6 begin:  4 1466304270.7061803
 7 begin:  5 1466304270.7061803
 8 end:    1 1466304271.7072382
 9 begin:  6 1466304271.7072382
10 end:    2 1466304272.7068274
11 begin:  7 1466304272.7068274
12 end:    3 1466304273.7075586
13 begin:  8 1466304273.707777
14 end:    4 1466304274.7071354
15 begin:  9 1466304274.7071354
16 end:    5 1466304275.7078736
17 end:    6 1466304277.7078269
18 end:    7 1466304279.7079546
19 end:    8 1466304281.7093537
20 end:    9 1466304283.7077909

 

可以看到1结束后,6马上运行

posted @ 2016-06-19 00:17  电神  阅读(233)  评论(0编辑  收藏  举报