1. 程式人生 > >python程序與執行緒混合使用例子

python程序與執行緒混合使用例子

from gevent import monkey; monkey.patch_all()
import gevent
import multiprocessing
import threading
import time


def thread_handle():
    print ("\033[1;33;42m now,begin handle thread task.......\033[0m")
    time.sleep(2)
    print("\033[1;31;42m now,end handle thread task.......\033[0m")


def process_handle():
    print ("now,begin handle process task.........")
    t_list = []
    for i in range(5):
        t = threading.Thread(target=thread_handle,)
        t.start()
        t_list.append(t)
    for i in t_list:
        i.join()




if __name__ == '__main__':
    p_list = []
    for i in range(10):
        p = multiprocessing.Process(target=process_handle,)
        p.start()
        p_list.append(p)
    for i in p_list:
        i.join()
    print ("now,every process task handle complete.........")