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

Neha Agarwal
  • 35
  • 1
  • 9

2 Answers2

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
Kallz
  • 3,244
  • 1
  • 20
  • 38
  • I get this error AttributeError: 'function' object has no attribute 'info' – Neha Agarwal Aug 21 '17 at 13:39
  • When i run the script what should i pass as arguments? As i get the device id later in script and how about logger – Neha Agarwal Aug 21 '17 at 13:44
  • 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 – Neha Agarwal Aug 22 '17 at 12:09
  • 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 – Neha Agarwal Aug 22 '17 at 12:09
  • @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' – Kallz Aug 22 '17 at 12:13
  • I am already in my scripts directory i run command something like [nagarwal@bernache testScripts]$ python ./Universals.py ./testAgreement.sh – Neha Agarwal Aug 22 '17 at 12:23
  • @NehaAgarwal as I already said that run it with full classified path of bash – Kallz Aug 22 '17 at 12:29
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.

yinnonsanders
  • 1,831
  • 11
  • 28