Skip to content

Commit 68fd60c

Browse files
committed
Add documentation on map_async
1 parent 08ab108 commit 68fd60c

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

docs/source/async.rst

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ This would also work with async-await syntax in Python 3
7272

7373
.. code-block:: python
7474
75+
import asyncio
7576
from streamz import Stream
76-
from tornado.ioloop import IOLoop
7777
7878
async def f():
7979
source = Stream(asynchronous=True) # tell the stream we're working asynchronously
@@ -82,7 +82,28 @@ This would also work with async-await syntax in Python 3
8282
for x in range(10):
8383
await source.emit(x)
8484
85-
IOLoop().run_sync(f)
85+
asyncio.run(f())
86+
87+
When working asynchronously, we can also map asynchronous functions.
88+
89+
.. code-block:: python
90+
91+
async def increment_async(x):
92+
""" A "long-running" increment function
93+
94+
Simulates a function that does real asyncio work.
95+
"""
96+
await asyncio.sleep(0.1)
97+
return x + 1
98+
99+
async def f_inc():
100+
source = Stream(asynchronous=True) # tell the stream we're working asynchronously
101+
source.map_async(increment_async).rate_limit(0.500).sink(write)
102+
103+
for x in range(10):
104+
await source.emit(x)
105+
106+
asyncio.run(f_inc())
86107
87108
88109
Event Loop on a Separate Thread

0 commit comments

Comments
 (0)