1

I want to print the output of each thread on different files. This is my code for threads :-

 def __init__(self, command):
        threading.Thread.__init__(self)
        self.command = command


def run(self):
        call(self.command) 

def get_devices():

       command_res = 'adb devices'
       results = os.popen(command_res, "r")
       command_result = ''
       while 1:
          line = results.readline()
          if not line: break
          command_result += line
       devices = command_result.partition('\n')[2].replace('n','').split('\tdevice')
       return [device for device in devices if len(device) > 2]

for device_id in device_ids :
    threads.append(Universal([sys.argv[1], device_id]))

for thread in threads:
    try:
        thread.start();
    except:
        print("Error: unable to start thread")

for thread in threads:
    thread.join();

Here device_ids is the list of my devices attached. Each device runs on separate thread. Is there a solution to do this in Python. Thanks in Advance

2 Answers 2

1

use logger for logging or write to file

  1. Create a function to get new logger with new file handler. At the

    import logging
    from threading import Thread
    import sys
    import subprocess
    
    device_ids = ["d1","d2","d3"]
    threads = []
    def get_logger(name, log_file, level=logging.INFO):
    
        handler = logging.FileHandler(log_file)        
        logger = logging.getLogger(name)
        logger.setLevel(level)
        logger.addHandler(handler)
    
        return logger
    
    
    class Universal(Thread):
        def __init__(self, command,device_id,logger):
            Thread.__init__(self)
            self.command = command
            self.logger = logger
            self.logger.info("thread instance init" + str(device_id))
    
        def run(self):
            self.logger.info("thread started" + str(device_id))
            subprocess.call(self.command) 
    
    
    for device_id in device_ids :
        name = str(device_id) 
        f_name = str(device_id) + str(".log")
        log = get_logger(name,f_name)
        threads.append(Universal(sys.argv[1], device_id,log))
    
    for thread in threads:
        try:
            thread.start();
        except:
            print("Error: unable to start thread")
    
    for thread in threads:
        thread.join();
    

Save it module a.py and run it with command

  python a.py ls

Output

 Public  Videos  d1.log  Desktop github Music Templates d2.log Doccuments gitlab Pictures  d3.log  Downloads torch

 Public  Videos  d1.log  Desktop github Music Templates d2.log Doccuments gitlab Pictures  d3.log  Downloads torch

 Public  Videos  d1.log  Desktop github Music Templates d2.log Doccuments gitlab Pictures  d3.log  Downloads torch
Sign up to request clarification or add additional context in comments.

7 Comments

I get this error AttributeError: 'function' object has no attribute 'info'
When i run the script what should i pass as arguments? As i get the device id later in script and how about logger
Actually i have other scripts in bash called in runtime which should run by this python script. When i run your code it do give me some thread specific files but i am not able to pass my bass scripts as arguments
I get this error...... Exception in thread Thread-2: Traceback (most recent call last): File "/usr/lib64/python2.7/threading.py", line 804, in bootstrap_inner self.run() File "./Universals.py", line 46, in run call(self.command) File "/usr/lib64/python2.7/subprocess.py", line 168, in call return Popen(*popenargs, **kwargs).wait() File "/usr/lib64/python2.7/subprocess.py", line 390, in __init errread, errwrite) File "/usr/lib64/python2.7/subprocess.py", line 1024, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory
@NehaAgarwal this error due to the path that you provide is incorrect. run python programme with full path like 'python universals.py /home/...path to bash'
|
0

The print call is not thread-safe, so you can either use the logging module and register a FileHandler for each thread, or use multiprocessing instead of threading, described here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.