擅长:python、mysql、java
<p>我在Jupyter中使用Asyncio的aha时刻如下所示:</p>
<pre><code>import time,asyncio
async def count():
print("count one")
await asyncio.sleep(1)
print("count four")
async def count_further():
print("count two")
await asyncio.sleep(1)
print("count five")
async def count_even_further():
print("count three")
await asyncio.sleep(1)
print("count six")
async def main():
await asyncio.gather(count(), count_further(), count_even_further())
s = time.perf_counter()
await main()
elapsed = time.perf_counter() - s
print(f"Script executed in {elapsed:0.2f} seconds.")
</code></pre>
<p>输出:</p>
<pre><code>count one
count two
count three
count four
count five
count six
Script executed in 1.00 seconds.
</code></pre>
<p>最初是从这里开始的,但一开始我并不清楚这个例子:
<a href="https://realpython.com/async-io-python/" rel="noreferrer">https://realpython.com/async-io-python/</a></p>