Tech Monger

Programming, Web Development and Computer Science.

Skip to main content| Skip to information by topic

Distributed TNSPING in Python with Example

Previously we have seen how can we implement TNSPING command in python and process tnsping response to make programmatic decisions. But that approach may not be optimal when you have large set of net service names to check. In this tutorial we will implement concurrent version of TNSPING checker in python with help of Threads and Queues.


Recommended Reading

Before proceeding with an example we would recommend you to read following posts. Code presented in this tutorial will build upon sequential tnsping and distributed processing technique of producer consumer model implemented in earlier posts.


Example

from threading import Thread
from Queue import Queue
from subprocess import Popen, PIPE


q = Queue()
final_result = {}

def store_data_source_names():
    # This function will act as a producer
    
    data_source_names = open('data_source_names.txt', 'rb').readlines()

    for data_source_name in data_source_names:
        data_source_name = data_source_name.strip()
        q.put(data_source_name)
        

def tnsping():
    # This function will act as a consumer
    
    while True:
        data_source_name = q.get()
        
        command_string = "tnsping {d}".format(d=data_source_name)

        # Converts tnsping command list
        command = command_string.split()

        # Trigger Command
        process = Popen(command, stdout=PIPE, stderr=PIPE)

        # Stores stdout and stderr of command execution
        stdout, stderr = process.communicate()

        # Stores tnsping exit code.
        # Code will be 0 if tnsping succeeds.
        status_code = process.returncode
        
        final_result[data_source_name] = (status_code, stdout, stderr)
        
        q.task_done()
        
        
for i in range(5):
    t = Thread(target=tnsping)
    t.daemon = True
    t.start()
    
store_data_source_names()

q.join()

Above program will expect new line separated data source names in filedata_source_names.txt and will check each data source name against tnsping command. It will store data source name as a key inside the dict final_result and value as a tuple containing exit code, output and error (if any) against the key.

To check tnsping success or failure of specific data source name say example_db you can use following code.

if final_result.get('example_db'):
    result = final_result.get('example_db')
    exit_code = result[0]
    if exit_code == 0:
        print "TNSPING Succeeded for example_db"
    else:
        print "TNSPING Failed for example_db"
else:
    print "example_db not found in data_source_names.txt"

Conclusion

This distributed way of checking tnsping is optimal when large number of database to be checked because sequential tnsping is slower due to being I/O bound. Using Threaded solution built upon producer consumer model will enhance speed and shorten the time taken for making checks.

Tagged Under : Linux Python