Introduction to Exeunt
Exeunt is a highly versatile and powerful tool designed for developers to efficiently manage and execute external processes and system commands. With its extensive set of APIs, Exeunt not only simplifies routine tasks but also enhances the capability to handle more complex operations seamlessly. This article provides a detailed overview of Exeunt, enriched with dozens of API examples to help you maximize its potential in your applications.
Core APIs
exec
The exec
API allows you to run external commands or scripts from within your application.
import exeunt
# Example: Running a simple shell command
result = exeunt.exec('ls -la')
print(result.stdout)
exec_async
The exec_async
API enables asynchronous execution of commands, allowing the program to continue running without waiting for the command to finish.
import exeunt
import asyncio
async def run_command():
result = await exeunt.exec_async('ping -c 3 example.com')
print(result.stdout)
asyncio.run(run_command())
exec_with_env
The exec_with_env
API allows you to set custom environment variables for the command being executed.
import exeunt
# Example: Running a command with custom environment variables
env_vars = {'MY_VAR': 'VALUE'}
result = exeunt.exec_with_env('printenv MY_VAR', env=env_vars)
print(result.stdout)
exec_with_timeout
The exec_with_timeout
API sets a timeout for the execution of a command, ensuring that it does not run indefinitely.
import exeunt
try:
result = exeunt.exec_with_timeout('sleep 10', timeout=5)
except exeunt.TimeoutExpired as e:
print('Command timed out')
kill_process
The kill_process
API provides a way to terminate processes programmatically.
import exeunt
# Example: Killing a running process
process = exeunt.exec_async('sleep 60')
exeunt.kill_process(process.pid)
print('Process killed')
Application Example
Let’s create a simple application using the exeunt
APIs to monitor and manage system processes.
import exeunt
import asyncio
async def monitor_system():
# List current directory contents
print('Listing directory contents:')
result = exeunt.exec('ls -la')
print(result.stdout)
# Asynchronously ping a website
print('Pinging website:')
result = await exeunt.exec_async('ping -c 3 example.com')
print(result.stdout)
# Run command with custom environment variable
print('Environment variable check:')
env_vars = {'MY_VAR': 'VALUE'}
result = exeunt.exec_with_env('printenv MY_VAR', env=env_vars)
print(result.stdout)
# Run command with timeout
try:
print('Running command with timeout:')
result = exeunt.exec_with_timeout('sleep 10', timeout=3)
except exeunt.TimeoutExpired:
print('Command timed out')
# Creating and killing a process
print('Creating and killing process:')
process = exeunt.exec_async('sleep 60')
exeunt.kill_process(process.pid)
asyncio.run(monitor_system())
With Exeunt, these simple yet powerful commands can be seamlessly integrated into your applications, facilitating process management, automation, and more.
Hash: 5fc6638eda8274b86dc1ec5665c7017af63ac7cf5ce388cf28ad2b59165f655d