Send and receive data with asyncio in another single thread
Project description
socket_asyncio_threaded
socket_asyncio_threaded, or sockco as I like to call it, is a python library to send and receive data with asyncio.StreamReader, asyncio.StreamWriter in another single thread.
Simple Usage
import socket_asyncio_threaded.client as sockco
if __name__ == '__main__':
client = sockco.SocketClient()
# Choose a ReceiveHandler from sockco like RH_Splitter, RH_HeaderSize,
# or implement your own handler
client.start_async(MOCK_SERVER_ADDR, MOCK_SERVER_PORT, sockco.RH_Splitter())
# Now do something you want and write data at any time
client.write_async(bytes(f'ping\n', encoding='utf-8'))
# When needed, just read a pack of data, no need to be concerned about when to receive
msg = client.read_async()
Background and Notes
This code is used in my project which needs to read and write data with socket occasionally. If only asyncio is used, the event loop needs to run in the main thread; and reading/writing data from different threads needs a little bit more resource than coroutine. So I decided to put them together, which is this library.
Some notes I'd like to mention:
-
Only client-side is available, and the implementation of server-side is under schedule.
-
The lifecycle of SocketClient is the same as StreamReader, so it is not allowed to write data after the receiving direction is closed, even if the sending direction is still open.
About
Version: 0.0.2
Auther: S3Studio
Url: https://github.com/S3Studio/socket_asyncio_threaded
New issues are welcomed
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file socket_asyncio_threaded-0.0.2.tar.gz
.
File metadata
- Download URL: socket_asyncio_threaded-0.0.2.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62408e0e973f8a393026c9fdd89c191163a96dac20f14eb80703d63c0b1e9d74 |
|
MD5 | 815421bb940717b39d383ce28d446dd0 |
|
BLAKE2b-256 | 8421bb3d1090aca8cd231735824427ef08768e75eca60de95d8a64e02b7afb9f |
File details
Details for the file socket_asyncio_threaded-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: socket_asyncio_threaded-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d499e7a1911df63cd86beed760cac54a0ef27d16c50fbd3ab9aaa82279bb538 |
|
MD5 | 70f3b47f2570c580c4b190618764f35c |
|
BLAKE2b-256 | 43db666406cee4bd914a226ba8cfc6bffc1773b283f9948778f8dda2ed7ed20a |