python 线程,进程28原则

本文介绍如何使用Python的threading和multiprocessing模块实现多线程和多进程任务,并通过具体的例子展示了线程和进程的创建及使用方法。此外还介绍了如何利用线程池和进程池来更高效地管理并发任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于函数实现

from threading import Thread


def fun(data, *args, **kwargs):
    """

    :param data:
    :param args:
    :param kwargs:
    :return:
    """
    print('start %s'%data)


if __name__ == "__main__":
    tups = ((1,),(2,),(3,))
    for i in tups:
        kwargs = {
            "target": fun,
            'args':i
        }
        t = Thread(**kwargs)
        t.start()  

基于类实现

from threading import Thread
from multiprocessing import Process


class MyThread(Thread):
    def __init__(self, func,*args):
        super(MyThread,self).__init__()
        self.func = func
        self.args = args

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


class MyProcess(Process):
    def __init__(self, func,*args):
        super(MyProcess, self).__init__()
        self.func = func
        self.args = args

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

  

传入类的入口函数,即可实现多线程

baidu = Baidu()
sogou = Sogou()
google = Google()

baiduThread = MyThread(baidu.run,1,100)
sogouThread = MyThread(sogou.run,1,100)
googleThread = MyThread(google.run,1,100)
# 线程开启
baiduThread.start()
sogouThread.start()
googleThread.start()
# 主线程等待子线程
baiduThread.join()
sogouThread.join()
googleThread.join()

  

总结一波,实际实现多进程的进程是类的实例化对象的run函数(baidu.run),因此在初始化类时是串行的,而很多模块并不能在同一进程中实例化多个对象。

例如:

class MyClass:
    def __init__(self):
        logging.basicConfig(filename='yaomaitong.log', filemode="w", level=logging.INFO) # 典型的反例

    def run(self,*args):
        # 所有需要多进程的程序都该写在这里
        self.start()
        self.continue()
        self.end()
        pass
    
    def start(self):
        pass
...

  

线程池、进程池:

from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExecutor

import requests


urls_list = [
    'https://www.baidu.com',
    'http://www.gaosiedu.com',
    'https://www.jd.com',
    'https://www.taobao.com',
    'https://news.baidu.com',
]
pool = ThreadPoolExecutor(3)

def request(url):
    response = requests.get(url)
    return response

def read_data(future,*args,**kwargs):
    response = future.result()
    response.encoding = 'utf-8'
    print(response.status_code,response.url)

def main():
    for url in urls_list:
        done = pool.submit(request,url)
        done.add_done_callback(read_data)

if __name__ == '__main__':
    main()
    pool.shutdown(wait=True)

  

转载于:https://www.cnblogs.com/zenan/p/9188521.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值