0

I have a long running python script which updated a list with a few "items"

I need to be able to query this running python script while its running to produce a print out of the content of this list.

Im not sure if this is possible on python2.6?

Added code below which basically generates a list of servers from log file. This is a basic script which ill make a lot more intrusive if i was able to dump the list it generates on a need to basis.

UPDATE

so ive got a script which does what i want in theory. The only thing ive noticed is that during the print from the "buf" received from client comes out on the main tool.

I cant rack my head around how to get it from the main tool to the client tool.

#!/usr/bin/python

import sys
import time
import re
import threading
import socket  
import errno

def handle():
  serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  PORT = 19999
  HOST = '127.0.0.1'
  MAX_LENGTH = 4096
  try:
     serversocket.bind((HOST, PORT))
  except socket.error, v:
     errorcode=v[0]
     print "Error found during startup [ %s ] " % (errorcode)
     sys.exit(0)
  serversocket.listen(10)
  (clientsocket, address) = serversocket.accept()
  while True:
    buf = clientsocket.recv(MAX_LENGTH)
    if buf == '': return #client terminated connection
    elif buf == "dump":
        for item in serverList:
                print item
    else:
        print "Unrecognised command entered"
        print buf

def follow(thefile):
    thefile.seek(0,2)
    while True:
        line = thefile.readline()
        if not line:
             time.sleep(0.1)
            continue
        yield line

handlethread = threading.Thread(target=handle)
handlethread.daemon = True
handlethread.start()

serverList = []

print "hello there"
logfile = open("/var/log/messages","r")
loglines = follow(logfile)
for line in loglines:
    servername=line.split(' ')[3]
    if servername not in serverList:
            serverList.append(servername)
            print "added %s to list of servers" % (servername)

CLIENT SCRIPT

#!/usr/bin/python
import socket
import sys


HOST = '127.0.0.1'
PORT = 19999
s = socket.socket()
s.connect((HOST, PORT))

while 1:
    msg = raw_input("Command To Send: ")
    if msg == "close":
       s.close()
       sys.exit(0)
    s.send(msg)
  • Please share the code that you've tried – Hari Krishnan Aug 14 '18 at 07:08
  • It is possible of course, but why you just do not write the content of the list on a file every some seconds or every time there are important changes then you would only need to do cat myfile.list and you are done. – Marco Aug 14 '18 at 07:17
  • the code ive got is pretty basic its just generating a list of server names found in a log file at present. The reason for the adhoc query is to get the list of whats in there. Id rather have it to the script memory than have a file updated every few seconds or when it changes. – richard.donnelly Aug 14 '18 at 08:36
  • updated my code to the latest. I literally only need to get the main script to send the data back to the client rather than print out to the output of the main one – richard.donnelly Aug 14 '18 at 13:32

2 Answers2

0

You could periodically save the contents of the list to your hard drive while the script is running. So in the main loop of the long-running script, simply Pickle the contents of the list. Whenever you need to check the values during execution just un-pickle the saved file. For more information on object serialization in Python see Pickle.

campellcl
  • 182
  • 2
  • 14
-1

If you're just trying to periodically print out the current list, you can just use the print function. Like so:

vals = []
for elem in range(10): 
    vals.append(elem)
    print vals