very simple way of inter-process communication
Project description
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 that will be used by socket internally, and you can set whatever number you like.
After api.publish is executed, a new thread will be generated and wait for 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を付け足すか、またはpublishのrun_forever引数をTrueにセットする必要があります。
process2に移りましょう。
ApiClientインスタンスを作ってprocess1に接続するだけです。
# in process2
from socpipe import *
api=ApiCient(port=50007)
print(api.add(1,2))
# 3 will be displayed
ところで、publishとApiClientはhostという引数も持っており、これをセットすることでリモートホストと通信することが出来ます。
ただ、この機能は未テストです。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file socpipe-0.0.6.zip.
File metadata
- Download URL: socpipe-0.0.6.zip
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d95b3d9d5df0d80ab025cd9916dbb7317321d4be2730254d1b3cf8f5b1f54d1e
|
|
| MD5 |
a9488917556f94cf4edb5160191fb00f
|
|
| BLAKE2b-256 |
a89c44ed4055cba7c18a8775cf6816d1b7b76c3945056a1787956ae49ffd530f
|