Giter Site home page Giter Site logo

simple-ipc's Introduction

simple-ipc

Inter-process communication based on stdio

Python Version PyPI Version License

A simple python interface for inter-process communication, a way to asynchronously exchange data with external programs at runtime. The internal mechanism functions by writing data to stdout and reading from stdin using multi threading.

Installation

pip install simple-ipc

Usage

Import the module first:

  • import ipc
  • from ipc import Worker

Parameters

Worker(command, callback=None, start=True, verbose=False)
  • command a path to an executable along with optional arguments

    • list: may include spaces
    • str: no support for spaces
  • callback (optional) a function that is called after new data has been received

    • must take exactly one argument
  • start (optional) start the worker automatically when created

  • verbose (optional) print status messages

Examples

worker = Worker(['with space.exe', 'spaced arg'], start=False)
with Worker('program.exe', verbose=True) as worker:
    ...
Worker('path/to/program.exe arg', lambda data: print(data))

A worker object

All data will be converted to type str internally.

Methods

  • start() starts the worker
  • send(data) sends the data to the external process
  • stop() initiates the termination of all threads and clears all data

Properties

  • running indicates the status of the worker (read-only)
  • data contains the most recent value (read-only)

Example program

from random import randint
from ipc import Worker

def process(data):
    if int(data) == 5:
        print('Process data...')

worker = Worker('program.exe', process)

while worker.running:
    number = randint(1, 10)
    worker.send(number)
    print(f'Data: {worker.data}')

More example code can be found here.

simple-ipc's People

Contributors

celltec avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

mfkiwl

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.