python多进程&多线程

python多进程&多线程

1.多进程定义

 

 

2.多线程

 

 3.应用

 

 3.1不使用多线程则顺序执行

 

 1 import logging
 2 from threading import Thread
 3 from time import sleep, ctime
 4 
 5 logging.basicConfig(level=logging.INFO)
 6 
 7 
 8 def loop0():
 9     logging.info("start loop0 at " + ctime())
10     sleep(4)
11     logging.info("end loop0 at " + ctime())
12 
13 
14 def loop1():
15     logging.info("start loop0 at " + ctime())
16     sleep(2)
17     logging.info("end loop0 at " + ctime())
18 
19 
20 def main():
21     logging.info("start all at " + ctime())
22     loop0()
23     loop1()
24     logging.info("end all at " + ctime())
25 
26 if __name__ == '__main__':
27     main()
不使用多线程--顺序执行

 

_thread没有守护线程的概念

 

 

import _thread
import logging
from threading import Thread
from time import sleep, ctime

logging.basicConfig(level=logging.INFO)

# def loop0():
#     logging.info("start loop0 at " + ctime())
#     sleep(4)
#     logging.info("end loop0 at " + ctime())
#
#
# def loop1():
#     logging.info("start loop1 at " + ctime())
#     sleep(2)
#     logging.info("end loop1 at " + ctime())


loops = [2, 4]


def loop(nloop, nsec, lock):
    '''
    :param nloop标记当前属于第几个loop
    :param nsec 睡眠几秒
    :param lock 加锁默认已加锁,
    '''
    logging.info("start loop1 at " + str(nloop) + ctime())
    sleep(nsec)
    logging.info("end loop1 at " + str(nloop) + ctime())
    lock.release()


def main():
    logging.info("start all at " + ctime())
    locks = []  ##存储所有的锁
    nloops =range(len(loops))  ##所有循环函数个数
    # for i in nloops:
    #     print(i)
    for i in nloops:
        lock = _thread.allocate_lock()  # 申明新的锁
        lock.acquire()  # 加锁操作
        locks.append(lock)  # locks获取了所有的锁
        # 此步骤是依次生成所有的锁
    for i in nloops:
        print(i)
        print(loops)
        print(loops[i])
        # 开启子线程
        _thread.start_new_thread(loop, (i, loops[i], locks[i]))  # i=nloop,loops[i]=nsec,lock[i]=lock
    for i in nloops:  # 依次判断锁是否解锁
        while locks[i].locked():
            pass

    logging.info("end all at " + ctime())


if __name__ == '__main__':
    main()
使用_thread模块

 

import logging
import threading

from time import sleep, ctime

logging.basicConfig(level=logging.INFO)

# def loop0():
#     logging.info("start loop0 at " + ctime())
#     sleep(4)
#     logging.info("end loop0 at " + ctime())
#
#
# def loop1():
#     logging.info("start loop1 at " + ctime())
#     sleep(2)
#     logging.info("end loop1 at " + ctime())


loops = [6, 2]


def loop(nloop, nsec):
    '''
    :param nloop标记当前属于第几个loop
    :param nsec 睡眠几秒
    :param lock 加锁默认已加锁,
    '''
    logging.info("start loop" + str(nloop) + " at " + ctime())
    sleep(nsec)
    logging.info("end loop" + str(nloop) + ' at ' + ctime())


def main():
    logging.info("start all at " + ctime())
    nloops = range(len(loops))  ##所有循环函数个数
    thread_list = []

    for i in nloops:
        t = threading.Thread(target=loop, args=(i, loops[i]))
        thread_list.append(t)

    for i in nloops:  # 依次判断锁是否解锁
        thread_list[i].start()
    for i in nloops:
        thread_list[i].join()
    logging.info("end end###### at " + ctime())


if __name__ == '__main__':
    main()
使用threading

 

import logging
import threading

from time import sleep, ctime

logging.basicConfig(level=logging.INFO)

loops = [6, 2]


class MyTread(threading.Thread):
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.func = func
        self.args=args
        self.name = name

    def run(self):
        self.func(*self.args)


def loop(nloop, nsec):
    '''
    :param nloop标记当前属于第几个loop
    :param nsec 睡眠几秒
    :param lock 加锁默认已加锁,
    '''
    logging.info("start loop" + str(nloop) + " at " + ctime())
    sleep(nsec)
    logging.info("end loop" + str(nloop) + ' at ' + ctime())


def main():
    logging.info("start all at " + ctime())
    nloops = range(len(loops))  ##所有循环函数个数
    thread_list = []

    for i in nloops:
        t = MyTread(loop, args=(i, loops[i]),name=loop.__name__)
        thread_list.append(t)

    for i in nloops:  # 依次判断锁是否解锁
        thread_list[i].start()
    for i in nloops:
        thread_list[i].join()
    logging.info("end end###### at " + ctime())


if __name__ == '__main__':
    main()
封装自己的Mythread

 

 

 

posted @ 2022-12-11 11:12  到我碗里来吧  阅读(19)  评论(0编辑  收藏  举报