Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Automating Appium Service Launch with Python Multithreading and Multiprocessing

Tech 3

Starting Appium Services with Python

Manually launching Apium services using DOS commands or batch files is inefficient. Automating this process can be achieved using Python's subprocess module, which creates new processes, connects to their input/output/error streams, and retrieves return codes.

Implementation Scenario

Launch two Appium servcies with the following port configurations:

  • Appium server port: 4723, bootstrap port: 4724
  • Appium server port: 4725, bootstrap port: 4726

Note: The bootstrap port (--bootstrap-port) facilitates communication between Appium and devices. Specifying this port is essential for multi-device script execution.

Create a new file in your project directory

Create a file named appium_launcher.py

import subprocess
from datetime import datetime

def launch_appium_service(server_host, server_port):
    # Bootstrap port enables Appium-device communication
    bootstrap_port = str(server_port + 1)
    command = f'start /b appium -a {server_host} -p {server_port} --bootstrap-port {bootstrap_port}'
    
    timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print(f'Executing: {command} at {timestamp}')
    
    log_file = f'../appium_logs/{server_port}.log'
    subprocess.Popen(command, shell=True, stdout=open(log_file, 'a'), stderr=subprocess.STDOUT)

if __name__ == '__main__':
    host_address = '127.0.0.1'
    port_number = 4723
    launch_appium_service(host_address, port_number)

Verification Steps

  1. In command prompt, run: netstat -ano | findstr 4723
  2. Check to log file creation at appium_logs/4723.log

Terminating Appium Services Use: taskkill -f -pid appium_process_id

Launching Multiple Appium Services

if __name__ == '__main__':
    host_address = '127.0.0.1'
    for iteration in range(2):
        current_port = 4723 + 2 * iteration
        launch_appium_service(host_address, current_port)

Concurrent Appium Service Launch with Multiprocessing

The previous approach doesn't execute Appium launches concurrently. Implement parallel execution using Python's multiprocesisng module.

Create parallel_appium_launch.py

import multiprocessing
import subprocess
from datetime import datetime

def initiate_appium_service(host_addr, port_num):
    bootstrap = str(port_num + 1)
    appium_command = f'start /b appium -a {host_addr} -p {port_num} --bootstrap-port {bootstrap}'
    
    current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print(f'Launching: {appium_command} at {current_time}')
    
    log_path = f'./appium_logs/{port_num}.log'
    subprocess.Popen(appium_command, shell=True, stdout=open(log_path, 'a'), stderr=subprocess.STDOUT)

# Prepare Appium process collection
appium_processes = []

# Configure Appium processes
for count in range(2):
    server_host = '127.0.0.1'
    server_port = 4723 + 2 * count
    appium_process = multiprocessing.Process(target=initiate_appium_service, args=(server_host, server_port))
    appium_processes.append(appium_process)

if __name__ == '__main__':
    # Concurrently start all Appium services
    for process in appium_processes:
        process.start()
    
    # Wait for all processes to complete
    for process in appium_processes:
        process.join()
Tags: Python

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.