Common API¶
-
class
aioftp.
StreamIO
(reader, writer, *, timeout=None, read_timeout=None, write_timeout=None)¶ Stream input/output wrapper with timeout.
- Parameters
reader (
asyncio.StreamReader
) – stream readerwriter (
asyncio.StreamWriter
) – stream writertimeout (
int
,float
orNone
) – socket timeout for read/write operationsread_timeout (
int
,float
orNone
) – socket timeout for read operations, overrides timeoutwrite_timeout (
int
,float
orNone
) – socket timeout for write operations, overrides timeout
-
close
()¶ Close connection.
-
read
(count=- 1)¶ -
Proxy for
asyncio.StreamReader.read()
.- Parameters
count (
int
) – block size for read operation
-
readexactly
(count)¶ -
Proxy for
asyncio.StreamReader.readexactly()
.- Parameters
count (
int
) – block size for read operation
-
readline
()¶ -
Proxy for
asyncio.StreamReader.readline()
.
-
write
(data)¶ -
Combination of
asyncio.StreamWriter.write()
andasyncio.StreamWriter.drain()
.- Parameters
data (
bytes
) – data to write
-
class
aioftp.
Throttle
(*, limit=None, reset_rate=10)¶ Throttle for streams.
- Parameters
-
append
(data, start)¶ Count data for throttle
-
clone
()¶ Clone throttle without memory
-
property
limit
¶ Throttle limit
-
async
wait
()¶ -
Wait until can do IO
-
class
aioftp.
StreamThrottle
(read, write)¶ Stream throttle with read and write
aioftp.Throttle
- Parameters
read (
aioftp.Throttle
) – stream read throttlewrite (
aioftp.Throttle
) – stream write throttle
-
clone
()¶ Clone throttles without memory
-
classmethod
from_limits
(read_speed_limit=None, write_speed_limit=None)¶ Simple wrapper for creation
aioftp.StreamThrottle
-
class
aioftp.
ThrottleStreamIO
(*args, throttles={}, **kwargs)¶ Bases:
aioftp.common.StreamIO
Throttled
aioftp.StreamIO
. ThrottleStreamIO is subclass ofaioftp.StreamIO
. throttles attribute is dictionary of name:aioftp.StreamThrottle
pairs- Parameters
*args –
positional arguments for
aioftp.StreamIO
**kwargs –
keyword arguments for
aioftp.StreamIO
throttles (
dict
withaioftp.Throttle
values) – dictionary of throttles
>>> self.stream = ThrottleStreamIO( ... reader, ... writer, ... throttles={ ... "main": StreamThrottle( ... read=Throttle(...), ... write=Throttle(...) ... ) ... }, ... timeout=timeout ... )
-
append
(name, data, start)¶ Update timeout for all throttles
-
iter_by_block
(count=8192)¶ Read/iterate stream by block.
- Return type
aioftp.AsyncStreamIterator
>>> async for block in stream.iter_by_block(block_size): ... ...
-
iter_by_line
()¶ Read/iterate stream by line.
- Return type
aioftp.AsyncStreamIterator
>>> async for line in stream.iter_by_line(): ... ...
-
async
read
(count=- 1)¶ -
aioftp.StreamIO.read()
proxy
-
async
readline
()¶
-
async
wait
(name)¶ -
Wait for all throttles
- Parameters
name (
str
) – name of throttle to acquire (“read” or “write”)
-
async
write
(data)¶ -
aioftp.StreamIO.write()
proxy
-
class
aioftp.
AsyncListerMixin
¶ Add ability to async for context to collect data to list via await.
>>> class Context(AsyncListerMixin): ... ... >>> results = await Context(...)
-
class
aioftp.
AbstractAsyncLister
(*, timeout=None)¶ Abstract context with ability to collect all iterables into
list
via await with optional timeout (viaaioftp.with_timeout()
)>>> class Lister(AbstractAsyncLister): ... ... @with_timeout ... async def __anext__(self): ... ...
>>> async for block in Lister(...): ... ...
>>> result = await Lister(...) >>> result [block, block, block, ...]
-
aioftp.
with_timeout
(name)¶ Method decorator, wraps method with
asyncio.wait_for()
. timeout argument takes from name decorator argument or “timeout”.- Parameters
name (
str
) – name of timeout attribute- Raises
asyncio.TimeoutError – if coroutine does not finished in timeout
Wait for self.timeout
>>> def __init__(self, ...): ... ... self.timeout = 1 ... ... @with_timeout ... async def foo(self, ...): ... ... pass
Wait for custom timeout
>>> def __init__(self, ...): ... ... self.foo_timeout = 1 ... ... @with_timeout("foo_timeout") ... async def foo(self, ...): ... ... pass
-
aioftp.
async_enterable
(f)¶ Decorator. Bring coroutine result up, so it can be used as async context
>>> async def foo(): ... ... ... ... return AsyncContextInstance(...) ... ... ctx = await foo() ... async with ctx: ... ... # do
>>> @async_enterable ... async def foo(): ... ... ... ... return AsyncContextInstance(...) ... ... async with foo() as ctx: ... ... # do ... ... ctx = await foo() ... async with ctx: ... ... # do
-
aioftp.
setlocale
(name)¶ Context manager with threading lock for set locale on enter, and set it back to original state on exit.
>>> with setlocale("C"): ... ...