Skip to main content

A python package for cross-script communication in a simple method

Project description


Python Script Communication

This script provides functions for communication between scripts using message passing. It includes a decorator listen to mark a function as a message listener and a function send to send messages to specific channels.

Usage

Init

Function to initialise a channel (create the files). run this before using the channel!

init(channel)

  • channel: an integer representing the channel to initialise

Listen

Decorator to make a function a message listener. The function needs a parameter for the message, which it will return as a dictionary.

@listen(channel, interval=1.0)
def listener_func(message):
    # Process the received message
  • channel: An integer representing the channel to listen to.
  • interval (optional): The interval in seconds to check for new messages (default is 1.0 second).

Send

Function to send a message to a specific channel.

send(channel, message, metadata={"source": main_file_name})

  • channel: An integer representing the channel to send the message to.
  • message: A dictionary containing the message data.
  • metadata (optional): A dictionary containing additional metadata for the message. By default, it includes the source file name where the send function is called.

cleanup

Function to clean up used files. WARNING! ONLY RUN THIS WHEN YOU ARE DONE USING THE PACKAGE! it WILL halt running scripts

cleanup()

Example

Here's an example of how to use the script:

from cscomms import messaging as cscomms

cscomms.init(1)

@cscomms.listen(channel=1, interval=0.5)
def message_listener(message):
  print(message)

cscomms.send(channel=1, message={"user": "username", "message": "message"})

In this example, the script listens to channel 1 and prints any received message. It then sends a message with the key-value pair {"key": "value"} to channel 1.


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

cscomms-0.0.2.tar.gz (3.4 kB view hashes)

Uploaded Source

Built Distribution

cscomms-0.0.2-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

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