asyncio आधारित API है। एक सर्वर से जुड़कर key अधिग्रहीत की जाती है और तुरंत रिलीज़ कर दी जाती है।
broker = TicketBroker.connect("127.0.0.1:5225")
try:
await broker.wait_ready(5)
ticket = await broker.acquire("key", wait=5, lease=30)
await ticket.release()
print("pass")
finally:
broker.close()कई asyncio टास्क खाता-वार लॉक से अन्योन्य बहिष्करण करते हुए जमा/निकासी को बार-बार दोहराएँ तो भी बैलेंस हमेशा शुरुआती मान पर लौटता है, इसकी पुष्टि करने वाला उदाहरण है। async with ticket उपयोग करने पर ब्लॉक से बाहर निकलते ही स्वचालित रूप से रिलीज़ हो जाता है।
async def transaction(broker: TicketBroker, account: int, delta: int) -> None:
key = f"account-{account}"
try:
ticket = await broker.acquire(key, WAIT, LEASE)
except Exception as e:
log.error("account %s acquire failed: %s", account, e)
return
async with ticket:
before = BALANCES[account]
await asyncio.sleep(0)
after = before + delta
BALANCES[account] = after
action, sign = ("withdraw", "-") if delta < 0 else ("deposit", "+")
log.info("account %s %s %s$%s -> $%s", account, action, sign, AMOUNT, after)पूर्ण उदाहरण test_example.py, test_bank.py, test_bench.py में देखें।