了解编程中的同步、异步和多线程

软文推广2个月前发布 刘老三
3 0

编程领域,了解处理任务的不同技术对于构建高效且响应迅速的系统至关重要。同步、异步和多线程软件开发中管理任务的三种常用方法。让我们通过咖啡店的类比来探讨这些概念。

了解编程中的同步、异步和多线程

同步编程:孤独的工作者

在同步编程中,任务以顺序方式一个接一个地执行。这就像让一名员工管理咖啡店运营的各个方面。

一步步:

1.接受订单:员工迎接每位顾客,接受他们的订单,并将其写下来。

2.制作饮料:接下来,同一位员工根据收到的订单准备每一种饮料,一次一种。

3.为顾客服务:最后,员工按照收到的顺序为顾客提供饮料。

示例代码:

def coffee_shop():
employee = Employee()
order = employee.take_order()
drink = employee.prepare_drink(order)
employee.serve_drink(drink)
class Employee:
def take_order(self):
print(“Employee takes the order from the customer.”)
# Code to take order
return order
def prepare_drink(self, order):
print(“Employee prepares the drink based on the order.”)
# Code to prepare drink
return drink
def serve_drink(self, drink):
print(“Employee serves the drink to the customer.”)
# Code to serve drink

coffee_shop()

谁在等待?

在同步编程中,必须先完成每个步骤,然后才能继续。员工(处理所有任务)等待每个操作完成后再继续。例如,员工等待每杯饮料准备好后再将其提供给顾客。

异步编程:员工团队

在异步编程中,任务可以彼此独立运行,从而允许并行执行。这就像让一个员工团队高效地合作来同时处理多个订单。

一步步:

接受订单:每个员工与不同的客户互动,同时接受他们的订单。

制作饮料:当一些员工接受订单时,其他员工根据收到的订单准备饮料。

为顾客服务:饮料准备好后,任何有空的员工都会将其提供给相应的顾客,而无需等待所有饮料都制作完成。

示例代码:

import asyncio
async def coffee_shop():
tasks = [asyncio.create_task(employee_workflow(employee)) for _ in range(3)]
await asyncio.gather(*tasks)
async def employee_workflow(employee):
order = await employee.take_order()
drink = await employee.prepare_drink(order)
await employee.serve_drink(drink)
class Employee:
async def take_order(self):
print(“Employee takes the order from the customer.”)
# Code to take order
return order
async def prepare_drink(self, order):
print(“Employee prepares the drink based on the order.”)
# Code to prepare drink
return drink
async def serve_drink(self, drink):
print(“Employee serves the drink to the customer.”)
# Code to serve drink
asyncio.run(coffee_shop())

谁在等待?

在异步编程中,任务独立进行,主程序不会等待每个任务完成后再继续。员工(处理不同的任务)不会互相等待。例如,当一名员工接受订单时,另一名员工可以同时准备饮料。

多线程:专业站

在多线程中,多个执行线程在单个进程中同时运行,每个线程处理不同的任务。这就像将咖啡店划分为专门的站点,每个站点都有专门的团队同时工作。

一步步:

接受订单:一个团队专注于在柜台接受顾客的订单。

制作饮料:另一个团队在厨房工作,根据收到的订单准备饮料。

为顾客提供服务:与此同时,第三个团队会在顾客准备好饮料后立即为他们提供饮料,而无需等待所有饮料都制作完成。

示例代码:

import threading

def coffee_shop():
order_station = OrderStation()
preparation_station = PreparationStation()
serving_station = ServingStation()
order_thread = threading.Thread(target=order_station.take_orders)
preparation_thread = threading.Thread(target=preparation_station.prepare_drinks)
serving_thread = threading.Thread(target=serving_station.serve_drinks)
order_thread.start()
preparation_thread.start()
serving_thread.start()
order_thread.join()
preparation_thread.join()
serving_thread.join()

class OrderStation:
def take_orders(self):
print(“Order station takes orders from customers.”)
# Code to take orders

class PreparationStation:
def prepare_drinks(self):
print(“Preparation station prepares drinks based on orders.”)
# Code to prepare drinks

class ServingStation:
def serve_drinks(self):
print(“Serving station serves drinks to customers.”)
# Code to serve drinks

coffee_shop()

谁在等待?

在多线程中,每个线程独立运行,主程序不会等待每个线程完成后再继续。专门的站点(每个站点由不同的团队处理)不会互相等待。例如,当一个团队接受订单时,另一团队可以同时准备饮料。

© 版权声明

相关文章