MultiStreamFramework

class MultiStreamFramework(apps, args)

Run multiple applications using multiprocessing.

This class provides a framework for running multiple applications concurrently in separate processes.

Instantiate and start a separate thread to monitor the exit event and stop the framework. Calling the stop method on the application object will close down the framework. The setting of the exit event can be done based on the application logic and is at the user’s discretion.

Typical Usage:

def wait_for_exit_cmd(app: MultiStreamFramework, exit_flag: multiprocessing.Event):
    exit_flag.wait()
    app.stop()

exit_flag = multiprocessing.Event()
app = MultiStreamFramework(
    apps=[App1, App2],
    args=[(app1_param1, exit_flag), (app2_param1, exit_flag)]
)
monitor_thread = threading.thread(target=wait_for_exit_cmd, args=(app, exit_flag))
monitor_thread.start()
try:
    app.start()
finally:
    # Set exit flag to close monitor_thread if it is still running
    exit_flag.set()
    monitor_thread.join()
Parameters
  • apps (List[Type[MultiStreamAppInterface]]) – List of Application classes to instantiate.

  • args (List[tuple]) – List of tuple of arguments for each application.

For an example of using the multi-stream framework, please refer the reference applications in the alwaysAI github repository.

start()

Start the MultiStreamFramework.

This method is a blocking call.

It unblocks or exits when an error occurs or stop() has been called.

Return type

None

Returns

None

Raises

Any exceptions or errors raised within the framework or the applications.

stop()

Stop the MultiStreamFramework.

This method is designed to be process and thread safe.

class MultiStreamAppInterface
run()

The function to be called to run the main loop of the application.

This class will be instantiated and run on a new process.