如何将Python并发和异步编程实例改写为一个长尾词的?
- 内容介绍
- 文章标签
- 相关推荐
本文共计834个文字,预计阅读时间需要4分钟。
关于并发、并行、同步阻塞、异步非阻塞、线程、进程、协程等概念,单从文字上理解往往较为困难,难以进行比较深刻的理解。本文将通过代码一步一步实现这些并发和异步编程,便于比较和掌握。
以下是一些基本的并发和异步编程实现示例:
1. 并发编程:使用Python的`threading`模块实现一个简单的并发示例。
pythonimport threading
def print_numbers(): for i in range(1, 6): print(i)
创建线程thread1=threading.Thread(target=print_numbers)thread2=threading.Thread(target=print_numbers)
启动线程thread1.start()thread2.start()
等待线程结束thread1.join()thread2.join()
2. 并行编程:使用Python的`multiprocessing`模块实现一个简单的并行示例。
pythonimport multiprocessing
def print_numbers(): for i in range(1, 6): print(i)
创建进程process1=multiprocessing.Process(target=print_numbers)process2=multiprocessing.Process(target=print_numbers)
启动进程process1.start()process2.start()
等待进程结束process1.join()process2.join()
3. 同步阻塞:使用`threading.Lock`实现线程同步。
pythonimport threading
lock=threading.Lock()
def print_numbers(): for i in range(1, 6): lock.acquire() # 获取锁 print(i) lock.release() # 释放锁
thread1=threading.Thread(target=print_numbers)thread2=threading.Thread(target=print_numbers)
thread1.start()thread2.start()
thread1.join()thread2.join()
4. 异步非阻塞:使用Python的`asyncio`模块实现一个简单的异步示例。
pythonimport asyncio
async def print_numbers(): for i in range(1, 6): print(i)
创建异步任务task=asyncio.create_task(print_numbers())
运行事件循环asyncio.run(task)
通过以上代码示例,我们可以逐步理解并发、并行、同步阻塞、异步非阻塞、线程、进程、协程等概念,并比较它们的异同。
关于并发、并行、同步阻塞、异步非阻塞、线程、进程、协程等这些概念,单纯通过文字恐怕很难有比较深刻的理解,本文就通过代码一步步实现这些并发和异步编程,并进行比较。解释器方面本文选择python3,毕竟python3才是python的未来,并且python3用原生的库实现协程已经非常方便了。
1、准备阶段
下面为所有测试代码所需要的包
#! python3 # coding:utf-8 import socket from concurrent import futures from selectors import DefaultSelector,EVENT_WRITE,EVENT_READ import asyncio import aiowww.sina.com' loop = asyncio.get_event_loop() async def fetch(url): async with aiohttp.ClientSession(loop=loop) as session: async with session.get(url) as response: response = await response.read() return response @tsfunc def asyncio_way(): tasks = [fetch(host+url) for url in urls_todo] loop.run_until_complete(asyncio.gather(*tasks)) return (len(tasks)) @tsfunc def sync_way(): res = [] for i in range(10): res.append(blocking_way()) return len(res) @tsfunc def process_way(): worker = 10 with futures.ProcessPoolExecutor(worker) as executor: futs = {executor.submit(blocking_way) for i in range(10)} return len([fut.result() for fut in futs]) @tsfunc def thread_way(): worker = 10 with futures.ThreadPoolExecutor(worker) as executor: futs = {executor.submit(blocking_way) for i in range(10)} return len([fut.result() for fut in futs]) @tsfunc def async_way(): res = [] for i in range(10): res.append(nonblocking_way()) return len(res) @tsfunc def callback_way(): for url in urls_todo: crawler = Crawler(url) crawler.fetch() loop1() @tsfunc def generate_way(): for url in urls_todo: crawler = Crawler2(url) Task(crawler.fetch()) loop1() if __name__ == '__main__': #sync_way() #process_way() #thread_way() #async_way() #callback_way() #generate_way() asyncio_way()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计834个文字,预计阅读时间需要4分钟。
关于并发、并行、同步阻塞、异步非阻塞、线程、进程、协程等概念,单从文字上理解往往较为困难,难以进行比较深刻的理解。本文将通过代码一步一步实现这些并发和异步编程,便于比较和掌握。
以下是一些基本的并发和异步编程实现示例:
1. 并发编程:使用Python的`threading`模块实现一个简单的并发示例。
pythonimport threading
def print_numbers(): for i in range(1, 6): print(i)
创建线程thread1=threading.Thread(target=print_numbers)thread2=threading.Thread(target=print_numbers)
启动线程thread1.start()thread2.start()
等待线程结束thread1.join()thread2.join()
2. 并行编程:使用Python的`multiprocessing`模块实现一个简单的并行示例。
pythonimport multiprocessing
def print_numbers(): for i in range(1, 6): print(i)
创建进程process1=multiprocessing.Process(target=print_numbers)process2=multiprocessing.Process(target=print_numbers)
启动进程process1.start()process2.start()
等待进程结束process1.join()process2.join()
3. 同步阻塞:使用`threading.Lock`实现线程同步。
pythonimport threading
lock=threading.Lock()
def print_numbers(): for i in range(1, 6): lock.acquire() # 获取锁 print(i) lock.release() # 释放锁
thread1=threading.Thread(target=print_numbers)thread2=threading.Thread(target=print_numbers)
thread1.start()thread2.start()
thread1.join()thread2.join()
4. 异步非阻塞:使用Python的`asyncio`模块实现一个简单的异步示例。
pythonimport asyncio
async def print_numbers(): for i in range(1, 6): print(i)
创建异步任务task=asyncio.create_task(print_numbers())
运行事件循环asyncio.run(task)
通过以上代码示例,我们可以逐步理解并发、并行、同步阻塞、异步非阻塞、线程、进程、协程等概念,并比较它们的异同。
关于并发、并行、同步阻塞、异步非阻塞、线程、进程、协程等这些概念,单纯通过文字恐怕很难有比较深刻的理解,本文就通过代码一步步实现这些并发和异步编程,并进行比较。解释器方面本文选择python3,毕竟python3才是python的未来,并且python3用原生的库实现协程已经非常方便了。
1、准备阶段
下面为所有测试代码所需要的包
#! python3 # coding:utf-8 import socket from concurrent import futures from selectors import DefaultSelector,EVENT_WRITE,EVENT_READ import asyncio import aiowww.sina.com' loop = asyncio.get_event_loop() async def fetch(url): async with aiohttp.ClientSession(loop=loop) as session: async with session.get(url) as response: response = await response.read() return response @tsfunc def asyncio_way(): tasks = [fetch(host+url) for url in urls_todo] loop.run_until_complete(asyncio.gather(*tasks)) return (len(tasks)) @tsfunc def sync_way(): res = [] for i in range(10): res.append(blocking_way()) return len(res) @tsfunc def process_way(): worker = 10 with futures.ProcessPoolExecutor(worker) as executor: futs = {executor.submit(blocking_way) for i in range(10)} return len([fut.result() for fut in futs]) @tsfunc def thread_way(): worker = 10 with futures.ThreadPoolExecutor(worker) as executor: futs = {executor.submit(blocking_way) for i in range(10)} return len([fut.result() for fut in futs]) @tsfunc def async_way(): res = [] for i in range(10): res.append(nonblocking_way()) return len(res) @tsfunc def callback_way(): for url in urls_todo: crawler = Crawler(url) crawler.fetch() loop1() @tsfunc def generate_way(): for url in urls_todo: crawler = Crawler2(url) Task(crawler.fetch()) loop1() if __name__ == '__main__': #sync_way() #process_way() #thread_way() #async_way() #callback_way() #generate_way() asyncio_way()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

