Skip to main content

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 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.1

Auther: S3Studio

Url: https://github.com/S3Studio/socket_asyncio_threaded

New issues are welcomed

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

socket_asyncio_threaded-0.0.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

socket_asyncio_threaded-0.0.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file socket_asyncio_threaded-0.0.1.tar.gz.

File metadata

  • Download URL: socket_asyncio_threaded-0.0.1.tar.gz
  • Upload date:
  • Size: 4.3 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

Hashes for socket_asyncio_threaded-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3659f5533729a30d63c2375dc3c7a1998ab96ee54fdaaad4cd69a1f26175fd16
MD5 f81afaacba5ceb243739c71b8043296f
BLAKE2b-256 df87b72bd108c6de1a1b4ccd7c912b15e0ac67845083f3227af7d23e3655170c

See more details on using hashes here.

File details

Details for the file socket_asyncio_threaded-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: socket_asyncio_threaded-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 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

Hashes for socket_asyncio_threaded-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fe9087740af46caabb7e00f4d601dbf84581d1e02e140b848795957f126ca35c
MD5 c5d048bf3e670307508ba86fae08dbcc
BLAKE2b-256 905563e353a541b3619762520d920cf15caa8b427432750ac1770b622e827037

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page