0

I want to create a process that has 3 sub processes, 2 to handle websockets and one to get input from terminal to pass to the other two sockets.

import sys
import time
import select
import asyncio

import threading

import multiprocessing as mp
from multiprocessing import Queue
from multiprocessing import Process


def managment_api():
    poller = select.poll()
    poller.register(sys.stdin, select.POLLIN)


    started = True
    count   = 0

    while started:
        print("management api [{:^6}]".format(count))

        while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
            _line = sys.stdin.readline()
            if _line:
                line = _line.strip().lower()
                if(line == "exit" or line == "quit"):
                    started = False
                    print("got exit message [{}]".format(started))
                else:
                  pass

        count += 1
        time.sleep(1)

def process_main(loop):
    print("process_main BEGIN")
    loop.run_in_executor(None, managment_api())
    print("process_main ENG  ")


if __name__ == "__main__":
    #this doesn't work
    main = Process(target=managment_api, args=())
    main.name = "management api"
    main.start()
    main.join()
    """

    #asyncio.run(managment_api()) #need to add async to management_api

When I make management_api an async function and call asyncio.run(management_api); I can get input.

If I try to run the same code without async in a separate process it, I get stuck in the while sys.stdin in selec... section of the code. I've tried with threads but that doesn't work either.

How can I run this code from a separate process, to get the input in another process?

1 Answer 1

1

I was able to solve the problem by first, using fn = sys.stdin.fileno() to get the main process file descriptor, passing that as an argument to the subprocess. Then using sys.stdin = os.fdopen(fn)

import sys
import time
import select
import asyncio

import threading

import multiprocessing as mp
from multiprocessing import Queue
from multiprocessing import Process


def managment_api(fn):
    sys.stdin = os.fdopen(fn)

    poller = select.poll()
    poller.register(sys.stdin, select.POLLIN)


    started = True
    count   = 0

    while started:
        print("management api [{:^6}]".format(count))

        while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
            _line = sys.stdin.readline()
            if _line:
                line = _line.strip().lower()
                if(line == "exit" or line == "quit"):
                    started = False
                    print("got exit message [{}]".format(started))
                else:
                  pass

        count += 1
        time.sleep(1)

def process_main(loop):
    print("process_main BEGIN")
    loop.run_in_executor(None, managment_api())
    print("process_main ENG  ")


if __name__ == "__main__":
    fn = sys.stdin.fileno()
    main = Process(target=managment_api, args=(fn, ))
    main.name = "management api"
    main.start()
    main.join()
    """

    #asyncio.run(managment_api()) #need to add async to management_api
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.