python多线程处理

进程

  • 进程是执行中的程序
  • 拥有独立地址空间、内存、数据栈等
  • 操作系统管理
  • 派生(fork或spawn)新进程
  • 进程间通信(IPC)方式共享信息
  • 并行执行(同一时刻有多个进程执行)

线程

  • 同进程下执行,并共享相同的上下文
  • 线程间的信息共享和通信更加容易
  • 多线程并发执行(每个时刻只有一个线程执行,对线程进行轮询执行)
  • 需要同步原语(锁、信号量)

python与线程

  • 解释器主循环
  • 主循环中只有一个控制线程在执行
  • 使用全局解释器锁(GIL)

两种线程管理

  • _thread:提供了基本的线程和锁
  • threading:提供了更高级别、功能更全面的线程管理
  • 支持同步机制
  • 支持守护线程

_thread-第一种实现

import _thread
import time
import logging
logging.basicConfig(level=logging.INFO)


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


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

def main():
    logging.info("start all at " + time.ctime())
    loop0()
    loop1()
    logging.info("end all at " + time.ctime())


def main():
    logging.info("start all at " + time.ctime())
    _thread.start_new_thread(loop0, ())
    _thread.start_new_thread(loop1, ())
    # _thread主线程退出后,所有的子线程会被quit掉,没有守护线程
    time.sleep(6)
    logging.info("end all at " + time.ctime())

if __name__ == '__main__':
    main()
INFO:root:start all at Sat Nov  7 16:31:20 2020
INFO:root:start loop0 at Sat Nov  7 16:31:20 2020
INFO:root:start loop1 at Sat Nov  7 16:31:20 2020
INFO:root:end loop1 at Sat Nov  7 16:31:22 2020
INFO:root:end loop0 at Sat Nov  7 16:31:24 2020
INFO:root:end all at Sat Nov  7 16:31:26 2020

_thread-第二种实现方法

import _thread
import time
import logging
logging.basicConfig(level=logging.INFO)


# 利用锁
loops = [2, 4]


def loop(nloop, nsec, lock):
    logging.info(f"start loop{nloop} at {time.ctime()}")
    time.sleep(nsec)
    logging.info(f"end loop{nloop} at {time.ctime()}")
    lock.release()


def main():
    logging.info("start all at " + time.ctime())
    locks = []
    nloops = range(len(loops))
    for i in nloops:
        lock = _thread.allocate_lock()
        lock.acquire()
        locks.append(lock)
    for i in nloops:
        _thread.start_new_thread(loop, (i, loops[i], locks[i]))
    for i in nloops:
        while locks[i].locked():
            pass
    logging.info("end all at " + time.ctime())

if __name__ == '__main__':
    main()
INFO:root:start all at Sat Nov  7 16:36:40 2020
INFO:root:start loop0 at Sat Nov  7 16:36:40 2020
INFO:root:start loop1 at Sat Nov  7 16:36:40 2020
INFO:root:end loop0 at Sat Nov  7 16:36:42 2020
INFO:root:end loop1 at Sat Nov  7 16:36:44 2020
INFO:root:end all at Sat Nov  7 16:36:44 2020

threading-第一种实现

import threading
import time
import logging
logging.basicConfig(level=logging.INFO)

loops = [2, 4]


def loop(nloop, nsec):
    logging.info(f"start loop{nloop} at {time.ctime()}")
    time.sleep(nsec)
    logging.info(f"end loop{nloop} at {time.ctime()}")


def main():
    logging.info("start all at " + time.ctime())
    threads = []
    nloops = range(len(loops))
    for i in nloops:
        t = threading.Thread(target=loop, args=(i,loops[i]))
        threads.append(t)
    for i in nloops:
        threads[i].start()
    for i in nloops:
        threads[i].join()
    logging.info("end all at " + time.ctime())


if __name__ == '__main__':
    main()

INFO:root:start all at Sat Nov  7 16:38:54 2020
INFO:root:start loop0 at Sat Nov  7 16:38:54 2020
INFO:root:start loop1 at Sat Nov  7 16:38:54 2020
INFO:root:end loop0 at Sat Nov  7 16:38:56 2020
INFO:root:end loop1 at Sat Nov  7 16:38:58 2020
INFO:root:end all at Sat Nov  7 16:38:58 2020

threading-第二种实现

import threading
import time
import logging
logging.basicConfig(level=logging.INFO)

loops = [2, 4]


class MyThread(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):
    logging.info(f"start loop{nloop} at {time.ctime()}")
    time.sleep(nsec)
    logging.info(f"end loop{nloop} at {time.ctime()}")


def main():
    logging.info("start all at " + time.ctime())
    threads = []
    nloops = range(len(loops))
    for i in nloops:
        t = MyThread(loop, (i, loops[i]), loop.__name__)
        threads.append(t)
    for i in nloops:
        threads[i].start()
    for i in nloops:
        threads[i].join()
    logging.info("end all at " + time.ctime())


if __name__ == '__main__':
    main()
INFO:root:start all at Sat Nov  7 16:40:20 2020
INFO:root:start loop0 at Sat Nov  7 16:40:20 2020
INFO:root:start loop1 at Sat Nov  7 16:40:20 2020
INFO:root:end loop0 at Sat Nov  7 16:40:22 2020
INFO:root:end loop1 at Sat Nov  7 16:40:24 2020
INFO:root:end all at Sat Nov  7 16:40:24 2020

原语

信号量

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值