run.py
Python Script for Concurrent Command Execution with Colored Output
Dependencies:
- subprocess: Used for running new applications or programs through Python.
- threading: Allows for running different parts of the program concurrently.
- os: Provides a way of using operating system dependent functionality.
- fcntl: Allows for file control and I/O operations on file descriptors.
- select: Provides a way to wait for I/O completion on multiple streams.
- colorama: A third-party module used for making ANSI escape character sequences work under MS Windows terminals.
- termcolor: A third-party module used for coloring terminal text.
Functions:
set_nonblocking(fd)
:- Sets the file descriptor
fd
to non-blocking mode.
- Sets the file descriptor
run_command(command, app_name, color)
:- Runs a shell command in a non-blocking manner using
subprocess.Popen
. - Sets
stdout
andstderr
of the process to non-blocking mode. - Continuously reads from the process’s output and error, printing anything that is emitted.
- Colors the output according to the specified
app_name
andcolor
. - Returns the process’s exit code once it finishes.
- Runs a shell command in a non-blocking manner using
run_server()
:- Wrapper function to run the server application.
run_client()
:- Wrapper function to run the client application.
Main Execution Flow:
- Initializes colorama for colored output compatibility across different operating systems.
- Defines a list of target functions representing different services to run concurrently (e.g., a web server and a client).
- Starts a new thread for each target function, running the corresponding commands concurrently.
- Waits for all threads to complete before exiting.