python - One asyncio program to manage many others -


recently i've been experimenting discord bots, use asyncio. i've been in process of making program controls lots of other bots, opening , closing them on fly, have problem; i've experimented subprocess.popen, rpyc , multiprocessing, i'm struggling work out how have communication between programs. i've tried using following line of code launch subprocesses:

popen('python smallprogram.py', stdout=pipe, stdin=pipe) 

but i'm still unable communicate main program smaller programs, due smaller ones needing run asyncio. prevents me using input() popen.communicate(). ideally, i'd way call function on smaller program when needed, small program still running asyncio. don't mind having paste same block of code each of smaller programs, think might solvable importations?

can done? i've never made api before, seems might need use api template. thank :)

note: need big->small communication, doing other way round nice too.

there many ways deal inter-process communication, , think use of stdin/stdout valid approach.

it turns out it's possible read asynchronously stdin in asyncio, though it's quite tricky do using standard library.

alternatively, can use aioconsole helper ainput:

import aioconsole  async def echo_child():     data = await aioconsole.ainput()     print(data, end='') 

or get_standard_streams interface similar stream api:

import aioconsole  async def echo_child():     stdin, stdout = await aioconsole.get_standard_streams()     data = await stdin.readline()     stdout.write(data) 

on parent side:

import asyncio  async def parent():     proc = await asyncio.create_subprocess_exec(         sys.executable, 'child.py',         stdin=asyncio.subprocess.pipe,         stdout=asyncio.subprocess.pipe)     proc.stdin.write(b'hello\n')     data = await proc.stdout.readline()     print(data.decode().strip())     proc.terminate() 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -