Skip to main content

bgpy

https://img.shields.io/pypi/v/bgpy.svg https://github.com/munterfi/bgpy/workflows/check/badge.svg Documentation Status https://codecov.io/gh/munterfi/bgpy/branch/master/graph/badge.svg

Running local or remote Python servers in the background using the subprocess module and establish stream socket-based communication with clients in both directions.

Features:

  • Start and initialize a server process with a simple Python script. Once this parent script is terminated, the server process continues to run in the background.

  • Send Python objects between the server and client processes (stored in a dict) without worrying about authentication, Python object serialization, setting up server and client sockets, message length, and chunksize in the network buffer.

  • Due to the socket-based communication between server and client, it is possible to resume the communication from any location, as long as access to the same network is given and the hostname and port on which the server is listening is known.

  • The communication between client and server is operating system independent (not like FIFO pipes for example). Furthermore, on Windows it is possible to communicate between the Windows Subsystem for Linux (WSL) and the Windows host system using bgpy.

  • Optionally start the server on the remote using the command line interface (bgpy server <host> <port>), and initialize it from the local client (initialize(host, port, init_task, exec_task, exit_task)) using Python.

Getting started

Install the stable release of the package from pypi:

pip install bgpy

Define tasks

Run and intialize a bgpy server on a host, which starts listening to the provided port. After starting the server, a INIT message with the init_task, exec_task() and exit_task() tasks are send to the server in order to complete the initialization.

  • Initialization task

Task that runs once during initialization and can be used to set up the server. The return value of this function must be a dict, which is then passed to the exec_task function with every request by a client.

def init_task(client_socket: ClientSocket) -> dict:
    init_args = {"request_count": 0, "value": 1000}
    return init_args
  • Execution task

Task that is called each time a request is made by a client to the server. In this task the message from the execute method of the Client class is interpreted and an action has to be defined accordingly. The input of the exec_task is the return value of the last exec_task function call (or if never called, the return value from the init_task). Using the function respond om the server, a second response can be sent to the client after the standard confirmation of the receipt of the message by the server.

def exec_task(
    client_socket: ClientSocket, init_args: dict, exec_args: dict
) -> dict:
    init_args["request_count"] += 1
    if exec_args["command"] == "increase":
            init_args["value"] += exec_args["value_change"]
    if exec_args["command"] == "decrease":
            init_args["value"] -= exec_args["value_change"]
    return init_args
  • Exit task

Task that is executed once if an exit message is sent to the server by the terminate method of the Client class. The input of the exit_task is the return value of the last exec_task function call (or if never called, the return value from the init_task). With respond a second message can be sent to the client, if the client is set to be waiting for a second response (Client.terminate(..., await_response=True).

def exit_task(
    client_socket: ClientSocket, init_args: dict, exit_args: dict
) -> None:
    init_args["request_count"] += 1
    init_args["status"] = "Exited."
    respond(client_socket, init_args)
    return None

Note: If the client is set to wait for a second response (Client.execute(..., await_response=True or Client.terminate(..., await_response=True) it is important to handle this on the server side by sending a response to the client using respond. Otherwise the client may be waiting forever as there is no timeout specified.

Run the server

Run an example background process on localhost and send requests using client sockets:

from bgpy import Client, Server
from bgpy.example.tasks import init_task, exec_task, exit_task

HOST = "127.0.0.1"
PORT = 54321

# Optionally set a token for the client authentication
from bgpy import token_create
TOKEN = token_create()

# Create server context
server = Server(host=HOST, port=PORT, token=TOKEN)

# Start server in background from context
server.run_background()

# Bind client to server context
client = Client(host=HOST, port=PORT, token=TOKEN)

# Send INIT message from client to server, receive OK
response = client.initialize(init_task, exec_task, exit_task)

# Execute command 'increase' with value on server, receive OK
response = client.execute({"command": "increase", "value_change": 10})

# Execute command 'decrease' with value on server, receive OK
response = client.execute({"command": "decrease", "value_change": 100})

# Terminate and wait for response, receive OK with values
response = client.terminate(await_response=True)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for bgpy 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bgpy 0.3.1
File Size Uploaded
bgpy-0.3.1.tar.gz 15.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bgpy 0.3.1
File Interpreter ABI Platform
bgpy-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 33.5 kB

Release files / bgpy-0.3.1.tar.gz

Download URL bgpy-0.3.1.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d1fbc8e154c4b615184003164fd9caa9bc11304b5b289b1c802f64d3448ed74e
BLAKE2b-256 checksum
How to use checksums
d6607f4ae89243c20b39f284698641f0e013491ff809a6cc6dee3458902c1b8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release files / bgpy-0.3.1-py3-none-any.whl

Download URL bgpy-0.3.1-py3-none-any.whl
Size 17.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1b8e9197b627539dadddf6c78d9dc4013aa5a935e806779aa308ab2ac8ac9172
BLAKE2b-256 checksum
How to use checksums
aee367b2fe296c1c60b52e52273e9c8a77a97b028b8be889bd1a01a35a6cb24e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page