title: Multi-threading in Python and shell
body: <p>I have a shell script called "nn.sh" which takes an ip address in a local network and SSH into that ip and then continuously read off some data from that ip and appends the results to a file called "log.txt" on the server. </p>
  
  <p>I need to write a Python code to run on the server which probably use multi-threading to run this script in one thread and then in another thread reads the values already available in the file "log.txt". How do I do this? </p>
  
  <p>I have written the following code:</p>
  
  <pre><code>#!/usr/bin/python
  import threading
  import time
  from subprocess import call, Popen, PIPE
  
  exitFlag = 0
  
  class loggerThread(threading.Thread):
      def __init__(self):
          threading.Thread.__init__(self)
          print "Logging thread started ..."
      def run(self):
          with open("log.txt","at") as log: 
                  call(['/bin/sh', 'nn.sh', '172.20.125.44', '10'], stdout = log)
  
  class readerThread(threading.Thread):
      def __init__(self):
          threading.Thread.__init__(self)
          print "Reading thread started ..."
      def run(self):
          while 1:
                  with open("log.txt","r") as log:
                          lines = log.read().split("\n")
                          print "Reader thread  ..."
                          print lines[-1]
  
  
  thread1 = loggerThread()
  thread2 = readerThread()
  
  thread1.start()
  thread2.start()
  </code></pre>
  
  <p>Here is also the contents of "nn.sh":</p>
  
  <pre><code>ssh -o IdentityFile=~/Dropbox/ALI/id_rsa -l root $1 &lt;&lt;EOF
      while :; 
          do date; 
             echo; 
             iwlist wlan0 scan 
             echo; 
             echo "#################################################################"; 
             sleep $2; 
          done;
  EOF
  </code></pre>
  
  <p>However if I run this code, nothing will be stored in "log.txt". Any ideas on how to fix this?  </p>