进程 线程笔记

来源:互联网 发布:自己实现java虚拟机 编辑:程序博客网 时间:2024/05/22 04:53

线程 直接调用

import threadingimport timedef sayhi(num):    print('Running on number: %s' % num)    time.sleep(3)if __name__ == '__main__':    t1 = threading.Thread(target=sayhi, args=(1,))    t2 = threading.Thread(target=sayhi, args=(2,))    t1.start()    t2.start()    print(t1.getName())    print(t2.getName())

线程 继承式调用

import threadingimport timeclass MyThread(threading.Thread):    def __init__(self, num):        threading.Thread.__init__(self)        self.num = num    def run(self):        print('Running on number: %s' % self.num)        time.sleep(3)if __name__ == '__main__':    t1 = MyThread(1)    t2 = MyThread(2)    t1.start()    t2.start()    print(t1.getName())    print(t2.getName())

有关join

import threadingimport timedef sayhi(num):    time.sleep(2)    print('After sleep in 2 secs')if __name__ == '__main__':    t1 = threading.Thread(target=sayhi, args=(1,))    t1.start()    print('In Main')# "C:\Program Files\Anaconda3\python.exe" D:/python/01/DAY08/t0801.py# In Main# After sleep in 2 secs# # Process finished with exit code 0

join即等待线程结束,如上所示,并没有join。
脚本即为主线程,t1则为分线程。故:
先打印# In Main
后打印# After sleep in 2 secs
加入join则输出如下:

import threadingimport timedef sayhi(num):    time.sleep(2)    print('After sleep in 2 secs')if __name__ == '__main__':    t1 = threading.Thread(target=sayhi, args=(1,))    t1.start()    t1.join()    print('In Main')# "C:\Program Files\Anaconda3\python.exe" D:/python/01/DAY08/t0801.py# After sleep in 2 secs# In Main## Process finished with exit code 0

守护线程

__author__ = 'Alex Li'import timeimport threadingdef run(n):    print('[%s]------running----\n' % n)    time.sleep(2)    print('--done--')def main():    for i in range(5):        t = threading.Thread(target=run, args=[i, ])        t.start()        # t.join(1)        print('starting thread', t.getName())m = threading.Thread(target=main, args=[])m.setDaemon(True)  # 将main线程设置为Daemon线程,它做为程序主线程的守护线程,当主线程退出时,m线程也会退出,由m启动的其它子线程会同时退出,不管是否执行完任务m.start()# m.join(timeout=2)print("---main thread done----")

上脚本的逻辑关系为,脚本所在主线程,生成子线程m 通过main方法生成5个子线程run
m为守护线程Daemon,当守护线程结束时,所有子线程也同时结束。

线程锁

import timeimport threadingdef addNum():    global num    print('--get num: ', num)    time.sleep(1)    lock.acquire()    num -= 1    lock.release()num = 100thread_list = []lock = threading.Lock()for i in range(100):    t = threading.Thread(target=addNum)    t.start()    thread_list.append(t)for i in thread_list:    i.join()print('final num:', num)

如上所示即线程锁的使用方法。

lock = threading.Lock()lock.acquire()num -= 1lock.release()

递归锁 RLock

import threading, timedef run1():    print("grab the first part data")    lock.acquire()    global num    num += 1    lock.release()    return numdef run2():    print("grab the second part data")    lock.acquire()    global num2    num2 += 1    lock.release()    return num2def run3():    lock.acquire()    res = run1()    print('--------between run1 and run2-----')    res2 = run2()    lock.release()    print(res, res2)if __name__ == '__main__':    num, num2 = 0, 0    lock = threading.RLock()    for i in range(10):        t = threading.Thread(target=run3)        t.start()while threading.active_count() != 1:    print(threading.active_count())else:    print('----all threads done---')    print(num, num2)

即,通过RLock能使锁成对生效。

lock = threading.RLock()lock.acquire()res = run1()print('--------between run1 and run2-----')res2 = run2()lock.release()

信号量 semaphore

import threading, timedef run(n):    semaphore.acquire()    time.sleep(1)    print("run the thread: %s\n" % n)    semaphore.release()if __name__ == '__main__':    num = 0    semaphore = threading.BoundedSemaphore(3)  # 最多允许5个线程同时运行    for i in range(20):        t = threading.Thread(target=run, args=(i,))        t.start()while threading.active_count() != 1:    pass  # print threading.active_count()else:    print('----all threads done---')    print(num)

信号量,即该锁同时允许多少线程占用。

    semaphore = threading.BoundedSemaphore(3)  # 最多允许5个线程同时运行    semaphore.acquire()    time.sleep(1)    print("run the thread: %s\n" % n)    semaphore.release()

线程 event

import threading,timeimport randomdef light():    if not event.isSet():        event.set() #wait就不阻塞 #绿灯状态    count = 0    while True:        if count < 10:            print('\033[42;1m--green light on---\033[0m')        elif count <13:            print('\033[43;1m--yellow light on---\033[0m')        elif count <20:            if event.isSet():                event.clear()            print('\033[41;1m--red light on---\033[0m')        else:            count = 0            event.set() #打开绿灯        time.sleep(1)        count +=1def car(n):    while 1:        time.sleep(1)        if  event.isSet(): #绿灯            print("car [%s] is running.." % n)        else:            print("car [%s] is waiting for the red light.." %n)            event.wait()if __name__ == '__main__':    event = threading.Event()    Light = threading.Thread(target=light)    Light.start()    for i in range(3):        t = threading.Thread(target=car,args=(i,))        t.start()

通过event实现多个线程同步运行状态

event = threading.Event()event.set() #wait就不阻塞 #绿灯状态event.clear()event.wait()

进程

创建进程

from multiprocessing import Processimport timedef f(name):    time.sleep(2)    print('hello', name)if __name__ == '__main__':    p = Process(target=f, args=('bob',))    p.start()    p.join()

父进行与子进程

输出显示了进程号的关系,注意进程号

from multiprocessing import Processimport osdef info(title):    print(title)    print('module name:', __name__)    print('parent process:', os.getppid())    print('process id:', os.getpid())    print("\n\n")def f(name):    info('\033[31;1mfunction f\033[0m')    print('hello', name)if __name__ == '__main__':    info('\033[32;1mmain process line\033[0m')    p = Process(target=info, args=('bob',))    p.start()    p.join()# "C:\Program Files\Anaconda3\python.exe" D:/python/01/DAY08/process_pid.py# main process line# module name: __main__# parent process: 2468# process id: 5380#### bob# module name: __mp_main__# parent process: 5380# process id: 6368##### Process finished with exit code 0

进程间通信

queue队列通信,单向先进先出

from multiprocessing import Process, Queuedef f(q):    q.put([42, None, 'hello'])if __name__ == '__main__':    q = Queue()    p = Process(target=f, args=(q,))    p.start()    print(q.get())  # prints "[42, None, 'hello']"    p.join()

由于每个进程都是独立的,之间不互通,故使用multiprocessing.Queue可完成进程间通信。将Queue作为参数即可传进去。


pipe管道通信,两个端口,都可以进出。

import multiprocessingdef f(conn):    conn.send([422, None, 'hello'])    conn.close()if __name__ == '__main__':    parent_conn, child_conn = multiprocessing.Pipe()    p = multiprocessing.Process(target=f, args=(child_conn,))    p.start()    print(parent_conn.recv())    p.join()

进程间数据共享

from multiprocessing import Process, Managerdef f(d, l):    d[1] = '1'    d['2'] = 2    d[0.25] = None    l.append(1)    print(l)if __name__ == '__main__':    with Manager() as manager:        d = manager.dict()        l = manager.list(range(5))        p_list = []        for i in range(10):            p = Process(target=f, args=(d, l))            p.start()            p_list.append(p)        for res in p_list:            res.join()        print(d)        print(l)

进程锁 Lock

from multiprocessing import Process, Lockdef 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()

进程池

from  multiprocessing import Process, Poolimport timedef Foo(i):    time.sleep(2)    return i + 100def Bar(arg):    print('-->exec done:', arg)if __name__ == '__main__':    #     freeze_support()    pool = Pool(5)    for i in range(10):        pool.apply_async(func=Foo, args=(i,), callback=Bar)        # pool.apply(func=Foo, args=(i,))    print('end')    pool.close()    pool.join()  # 进程池中进程执行完毕后再关闭,如果注释,那么程序直接关闭。
0 0