Comments (3)
Hi @tiptop96,
The perserve
operator doesn't not preserve the content of the stream but prevents the stream from being closed when exiting the streaming context. In order to preserve the content, you can add the data to a list and later chain this list with the preserved iterator:
async def main():
async with aiohttp.ClientSession() as sesh:
async with sesh.get("https://jsonplaceholder.typicode.com/todos/1") as resp:
async with stream.preserve(resp.content).stream() as streamer:
stack = []
async for line in streamer:
stack.append(line)
await asyncio.sleep(1)
# Some criteria in the middle of content
if b"title" in line:
break
preserved_content = stream.iterate(stack) + resp.content
async with preserved_content.stream() as streamer:
async for line in streamer:
print(line)
await asyncio.sleep(1)
You can also factorize this logic into a dedicated operator:
@operator(pipable=True)
async def preserve_content(source, items):
for item in items:
yield item
async with stream.preserve(source).stream() as streamer:
async for item in streamer:
items.append(item)
yield item
async def main():
async with aiohttp.ClientSession() as sesh:
async with sesh.get("https://jsonplaceholder.typicode.com/todos/1") as resp:
items = []
preserved = preserve_content(resp.content, items)
async with preserved.stream() as streamer:
async for line in streamer:
await asyncio.sleep(1)
# Some criteria in the middle of content
if b"title" in line:
break
async with preserved.stream() as streamer:
async for line in streamer:
print(line)
await asyncio.sleep(1)
As a side note, I also receive a warning as follows: [...]
The stream operators are classes that are created dynamically, so I'm not surprised that linters might struggle to detect their interface. There might be a way to help them figure out the right info, I'll try to look into it later.
Hope it helps :)
from aiostream.
Ahh my bad for missunderstanding the operator! But this is awesome, thanks so much for the thorough response and the awesome lib! 🔥
from aiostream.
No problem :)
from aiostream.
Related Issues (20)
- Coroutine function detection procudes false negatives
- How to dispatch items from a single source to several concurrent streams? HOT 2
- Idea: Broadcasting to multiple consumers simultaneously HOT 5
- RuntimeError: athrow(): asynchronous generator is already running HOT 2
- aiter-like creation operator HOT 3
- Unexpected error when returning from merged stream HOT 6
- Async class methods in pipeline HOT 7
- Rate limit / throttle operator HOT 19
- Task exception was never retrieved warning HOT 3
- Cancelling pending tasks in same TaskGroup causes RuntimeError HOT 3
- UnboundLocalError when trying to reuse a stream HOT 1
- iterating from a synchronous iterator blocks the event loop HOT 6
- add support python3.10 HOT 1
- Please upload a wheel release to pypi HOT 2
- `stream.list` implementation is x10 slower than using plain Python built-in functionality (list comprehension) HOT 2
- Idea: Timeouts on chunks HOT 4
- action: support task_limit HOT 5
- Regarding `update_pipe_module` HOT 3
- Aiostream fails to import with a TypeError HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aiostream.