zoukankan      html  css  js  c++  java
  • 进程和线程

    1、开启进程的两种方式

    2、开启线程的两种方式

    3、进程池

    4、线程池


    1、开启进程的两种方式

    #方式一
    from multiprocessing import Process
    import time
    
    def task(name):
        print('%s is running' %name)
        time.sleep(5)
        print('%s is done' %name)
    
    if __name__ == '__main__':
        p=Process(target=task,args=('alex',))
        p.start()
        print('')
    #方式二
    from multiprocessing import Process
    import time
    
    class MyProcess(Process):
        def __init__(self,name):
            super(MyProcess,self).__init__()
            self.name=name
            
        def run(self):
            print('%s is running' %self.name)
            time.sleep(3)
            print('%s is done' %self.name)
    
    if __name__ == '__main__':
        p=MyProcess('进程1')
        p.start() #p.run()
        print('')

    2、开启线程的两种方式

    #方式一
    from threading import Thread
    import time
    import random
    
    def piao(name):
        print('%s is piaoing' %name)
        time.sleep(random.randint(1,3))
        print('%s is piao end' %name)
    
    if __name__ == '__main__':
        t1=Thread(target=piao,args=('alex',))
        t1.start()
        print('')
    #方式二
    from threading import Thread
    import time
    import random
    
    class MyThread(Thread):
        def __init__(self,name):
            super().__init__()
            self.name=name
    
        def run(self):
            print('%s is piaoing' %self.name)
            time.sleep(random.randint(1,3))
            print('%s is piao end' %self.name)
    
    if __name__ == '__main__':
        t1=MyThread('alex')
        t1.start()
        print('')

    3、进程池

    #异步调用:
    from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
    import time,random,os
    
    def task(n):
        print('%s is ruuning' %os.getpid())
        time.sleep(random.randint(1,3))
        # res=n**2
        # handle(res)
        return n**2
    
    def handle(res):
        res=res.result()
        print('handle res %s' %res)
    
    if __name__ == '__main__':
        #异步调用
        pool=ProcessPoolExecutor(2)
    
        for i in range(5):
            obj=pool.submit(task,i)
            obj.add_done_callback(handle) #回调函数handle(obj)
    
        pool.shutdown(wait=True) #相当于进程池的pool.close()+pool.join()操作
        print('')

    4、线程池

    from concurrent.futures import ThreadPoolExecutor
    from threading import current_thread
    import requests
    import time
    
    def get(url):
        print('%s GET %s' %(current_thread().getName(),url))
        response=requests.get(url)
        time.sleep(2)
        if response.status_code == 200:
            return {'url':url,'content':response.text}
    
    def parse(res):
        res=res.result()
        print('parse:[%s] res:[%s]' %(res['url'],len(res['content'])))
    
    
    if __name__ == '__main__':
        pool=ThreadPoolExecutor(2) #线程池是只能开2个线程,2个线程来接受客人 默认是CPU核数的5倍
    
        urls=[
            'https://www.baidu.com',
            'https://www.python.org',
            'https://www.openstack.org',
            'https://www.openstack.org',
        ]
        for url in urls:
            pool.submit(get,url).add_done_callback(parse) #回调函数
    
        pool.shutdown(wait=True)
  • 相关阅读:
    经典算法系列二-归并排序
    经典算法系列一-快速排序
    u-boot,linux,文件系统移植笔记1
    ARM函数调用时参数传递规则
    内核移植 nand分区
    LINUX的patch文件打patch
    idea插件使用
    socket通信同步通信,异步通信
    今天学了一个很简易的测试数据库是否连接成功
    wpf中TreeView的使用
  • 原文地址:https://www.cnblogs.com/snailgirl/p/8990659.html
Copyright © 2011-2022 走看看