Professional Documents
Culture Documents
web3.py Patterns_ Intro to Async
web3.py Patterns_ Intro to Async
Web3.py
Asynchronous support has long been one of the most requested features in
web3.py. The AsyncHTTPProvider was first introduced in v5.20.0, but has
received polish in most releases since. There are a couple edge cases to go for full
support, but the freshly released v6 is as good a time as any to give the async
provider a try!
Why async?
If you're satisfied with the performance of your web3.py dapp or script today,
you're under no obligation to update it. However, dapps very commonly spend
much of their runtime waiting on requests to make the round trip to and from a
remote node to query block data. Asynchronous programming patterns can make
much more efficient use of your computing resources, dispatching additional
requests while still waiting for earlier ones to return, for example. If your
application performs a lot of data queries, you may have a good candidate for a
provider refactor.
A code sample
Let's skip straight to the good stuff. Here's a simple, contrived example that
makes 50 requests – one for each of the first 50 blocks of the Ethereum
blockchain – and logs out the block number as each request returns.
import asyncio
from web3 import AsyncWeb3, AsyncHTTPProvider
w3 = AsyncWeb3(AsyncHTTPProvider('https://...'))
asyncio.run(fetch_blocks(50))
# 30
# 8
# 24
# 13
# ...
If you need a random number generator, there are certainly more efficient ways
to go about that. Hopefully though, this is a helpful illustration of asynchronous
behavior. Depending on your use case, you may need to reorder the data before
you make use of it. The gather , wait , or callback methods may help with that;
a deep rabbit hole of learning awaits those who need more sophisticated patterns.
AsyncENS
For the uninitiated, an ENS module exists within web3.py. It too has been given
asynchronous superpowers via the AsyncENS class:
w3 = AsyncWeb3(AsyncHTTPProvider('https://...'))
https://snakecharmers.ethereum.org/web3-py-patterns-intro-async/ 1/3
5/30/24, 8:07 PM web3.py Patterns: Intro to Async
ns = AsyncENS.from_web3(w3)
asyncio.run(fetch_addresses())
Performance
Your optimization gains will vary greatly depending on your use case. Again, you
will benefit most in cases where you spend much of your runtime waiting on
responses from remote nodes. To give you a sense of the scale, here are some
benchmarks while using a free remote node service.
Wrapping up
While continuing to receive polish, the AsyncHTTPProvider is ready for use
today. You can expect your query-intensive applications to gain some serious
performance improvements by adopting these asynchronous programming
patterns. Looking for support or to share what you're building? Join the
Ethereum Python Community Discord.
https://snakecharmers.ethereum.org/web3-py-patterns-intro-async/ 2/3
5/30/24, 8:07 PM web3.py Patterns: Intro to Async
Powered by Ghost
https://snakecharmers.ethereum.org/web3-py-patterns-intro-async/ 3/3