Issue
In my app I'd like to have an an async output channel to write data which is actually written by an existing API (https://github.com/ynqa/pandavro). Unfortunately the interface of the asyncio writer doesn't conform to the interface of the classic file-like writer.
Is there a way, like in Java, to somehow wrap the asyncio channel within a classic FileWriter interface? (w/o writing the wrapper by my own ...)
This is the code which I'd like to get running:
import numpy as np
import pandas as pd
import pandavro as pdx
import asyncio
async def main():
df = pd.DataFrame({
"Boolean": [True, False, True, False],
"Float64": np.random.randn(4),
"Int64": np.random.randint(0, 10, 4),
"String": ['foo', 'bar', 'foo', 'bar'],
"DateTime64": [pd.Timestamp('20190101'), pd.Timestamp('20190102'),
pd.Timestamp('20190103'), pd.Timestamp('20190104')]})
reader, writer = await asyncio.open_connection('127.0.0.1', 9090)
pdx.to_avro(writer, df)
writer.close()
await writer.wait_closed()
if __name__ == '__main__':
asyncio.run(main())
Solution
The only solution I have found by myself is to write a simple wrapper class:
class ChannelWriter:
__delegate = None
def __init__(self, delegate: asyncio.streams.StreamWriter):
self.__delegate = delegate
async def close(self):
await self.__delegate.drain()
return self.__delegate.close()
@staticmethod
def seekable():
return False
def write(self, value):
self.__delegate.write(value)
def flush(self):
pass
Answered By - cokeSchlumpf
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.