进程和线程

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)

 

posted on 2018-05-04 15:01  蜗牛也是妞  阅读(204)  评论(0编辑  收藏  举报