import asyncio
import threading
# 银行账户类
class BankAccount:
def __init__(self, initial_balance=0):
self.balance = initial_balance
self.lock = threading.Lock() # 使用锁来避免竞态条件
# 存款方法
async def deposit(self, amount):
with self.lock:
self.balance += amount
print(f"存款 {amount} 成功,当前余额: {self.balance}")
# 取款方法
async def withdraw(self, amount):
with self.lock:
if self.balance >= amount:
self.balance -= amount
print(f"取款 {amount} 成功,当前余额: {self.balance}")
else:
print("余额不足,取款失败")
# 模拟处理客户请求
async def handle_customer(account, request_type, amount):
if request_type == "deposit":
await account.deposit(amount)
elif request_type == "withdraw":
await account.withdraw(amount)
# 主函数
async def main():
account = BankAccount()
requests = [
("deposit", 100),
("withdraw", 50),
("deposit", 200),
("withdraw", 300)
]
tasks = []
for request_type, amount in requests:
task = asyncio.create_task(handle_customer(account, request_type, amount))
tasks.append(task)
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())
BankAccount
类:
__init__
方法初始化账户余额,并创建一个线程锁lock
用于同步访问账户余额。
deposit
方法实现存款逻辑,使用with self.lock
语句来获取锁,确保在存款操作期间其他线程无法修改余额,操作完成后自动释放锁。
withdraw
方法实现取款逻辑,同样使用锁来保证数据一致性,并且在取款前检查余额是否足够。
handle_customer
函数:
- 根据请求类型(存款或取款)调用账户对应的方法来处理客户请求。
main
函数:
- 创建一个
BankAccount
实例。
- 定义一系列客户请求。
- 使用
asyncio.create_task
创建任务来并发处理每个客户请求,并将任务添加到tasks
列表中。
- 最后使用
asyncio.gather
等待所有任务完成。
if __name__ == "__main__"
:
- 使用
asyncio.run
来运行main
异步函数。