import threading
from time import sleep, ctime
loop = [4, 2]
class ThreadFunc:
    def __init__(self, name):
        self.name = name
    def loop(self, nloop, nsec):
        '''
        :param nloop: loop函数的名称
        :param nsec: 系统休眠时间
        :return:
        '''
        print('Start loop ', nloop, 'at ', ctime())
        sleep(nsec)
        print('Done loop ', nloop, ' at ', ctime())
def main():
    print("Starting at: ", ctime())
    # ThreadFunc("loop").loop 跟一下两个式子相等:
    # t = ThreadFunc("loop")
    # t.loop
    # 以下t1 和  t2的定义方式相等
    t = ThreadFunc("loop")
    t1 = threading.Thread( target = t.loop, args=("LOOP1", 4))
    # 下面这种写法更西方人,工业化一点
    t2 = threading.Thread( target = ThreadFunc('loop').loop, args=("LOOP2", 2))
    # 常见错误写法
    #t1 = threading.Thread(target=ThreadFunc('loop').loop(100,4))
    #t2 = threading.Thread(target=ThreadFunc('loop').loop(100,2))
    t1.start()
    t2.start()
    t1.join( )
    t2.join()
    print("ALL done at: ", ctime())
if __name__ == '__main__':
    main()输出>>>
Starting at: Sun Sep 2 10:04:47 2018 Start loop LOOP1 at Sun Sep 2 10:04:47 2018 Start loop LOOP2 at Sun Sep 2 10:04:47 2018 Done loop LOOP2 at Sun Sep 2 10:04:49 2018 Done loop LOOP1 at Sun Sep 2 10:04:51 2018 ALL done at: Sun Sep 2 10:04:51 2018
分析:注意:实例化时threading.Thread(target=xxx, args=(xxx,))格式完整,工业风写法为init了的类的函数,args为其余param,一行搞定喵
threading.Lock()的两个线程,竞争资源都acquire(),造成无法release(),最后无法继续程序。threading.Semaphore(n)n=允许同时运行线程数threading.Timer(t, func)指定时间开始线程multiprocessing.Process()直接生成进程__init__里用super().__init__run()os.getppid()得到父进程id,用os.getpid()得到本进程idexample:
import multiprocessing
from time import ctime
def consumer(input_q):
    print("Into consumer:", ctime())
    while True:
        item = input_q.get()
        if item is None:
            break
        print("pull", item, "out of q")
    print("Out of consumer:", ctime())
def producer(sequence, output_q):
    for item in sequence:
        print("Into procuder:", ctime())
        output_q.put(item)
        print("Out of procuder:", ctime())
if __name__ == '__main__':
    q = multiprocessing.Queue()
    cons_p1 = multiprocessing.Process(target=consumer, args=(q,))
    cons_p1.start()
    cons_p2 = multiprocessing.Process (target=consumer, args=(q,))
    cons_p2.start()
    sequence = [1, 2, 3, 4]
    producer(sequence, q)
    q.put(None)
    q.put(None)
    cons_p1.join()
    cons_p2.join()Into procuder: Tue Sep 4 15:57:37 2018 Out of procuder: Tue Sep 4 15:57:37 2018 Into procuder: Tue Sep 4 15:57:37 2018 Out of procuder: Tue Sep 4 15:57:37 2018 Into procuder: Tue Sep 4 15:57:37 2018 Out of procuder: Tue Sep 4 15:57:37 2018 Into procuder: Tue Sep 4 15:57:37 2018 Out of procuder: Tue Sep 4 15:57:37 2018 Into consumer: Tue Sep 4 15:57:37 2018 pull 1 out of q pull 2 out of q pull 3 out of q pull 4 out of q Out of consumer: Tue Sep 4 15:57:37 2018 Into consumer: Tue Sep 4 15:57:37 2018 Out of consumer: Tue Sep 4 15:57:37 2018
分析:
multiprocessing.Queue()建立一个进程间队列Queue.put()以及Queue.get()为队列操作,先进先出