ryo-ma / socpipe

very simple way of inter-process communication

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

overview

soctype provides very simple ways of inter-process communication.
This only uses socket,json and threading internally, which are standard modules.

installation

pip install socpipe or pip3 install socpipe

usage

First, please define some functions like below:

# in process1

def add(a,b):
  return a+b

Then, please create ApiServer instance and make your APIs visible to other processes.

# in process1

from socpipe import *
api=ApiServer(apis=[add])
api.publish(port=50007)

while True:
  pass

where port is the port number which will be used by socket internally, and you can set whatever number you like.
After api.publish is executed, new thread will be generated and wait connections.
So you need to prevent process1 from exiting by while True or setting run_forever parameter of publish True.

Let's move on to process2.
All you have to do is to connect process1 by creating ApiClient instance.

# in process2

from socpipe import *
api=ApiCient(port=50007)
print(api.add(1,2))
# 3 will be displayed

By the way, publish and ApiClient also has a parameter host, so you can communicate with remote hosts by setting this parameter.
I haven't tested this function, though.

概要

socpipeはプロセス間通信のための大変シンプルな関数を提供します。
このモジュールは内部で標準モジュールであるsocket,json,threadingのみを使用しています。

インストール

pip install socpipe または pip3 install socpipe

使い方

まず、次のように適当な関数を定義して下さい。

# in process1

def add(a,b):
  return a+b

そして、ApiServerインスタンスを作り、あなたのAPIを他のプロセスに公開します。

# in process1

from socpipe import *
api=ApiServer(apis=[add])
api.publish(port=50007)

while True:
  pass

ここで、portは内部でsocketによって使われるポート番号であり、好きな番号をセットして構いません。
api.publishが実行されると、新しいスレッドが生成され他のプロセスからの接続を待つようになります。
よって、process1が終了してしまわないように、while Trueを付け足すか、またはpublishrun_forever引数をTrueにセットする必要があります。

process2に移りましょう。 ApiClientインスタンスを作ってprocess1に接続するだけです。

# in process2

from socpipe import *
api=ApiCient(port=50007)
print(api.add(1,2))
# 3 will be displayed

ところで、publishApiClienthostという引数も持っており、これをセットすることでリモートホストと通信することが出来ます。
ただ、この機能は未テストです。

About

very simple way of inter-process communication

License:MIT License


Languages

Language:Python 100.0%