Hi all,

I have a file which have ip's of three servers say servers.txt. I am opening the file for every request. Instead I want to load the file for the first request and from the next time onwards. I have to avoid file I/O. How can I achieve this using python. And also I check for the health of servers each day to determine which is alive and which is down and accordingly add/remove server ip's. How can make my to load new ip's once modification is done??

I hope i am making sense

regards
kiran

The first part of your question isn't very hard. To load the ip addresses once and use them for the rest of your program

data = open('ipaddresslist.txt','r')
listofipaddresses = []

for line in data.readlines():
    listofipaddresses.append(line.strip())

data.close()

Now for the second part. It sounds like you want to monitor the file for changes and incorporate those changes. The only thing I can come up with is to have a the program repeat the process above every few minutes. Or you could do what you're doing now, I cant believe that the file I/O is that bad. Unless you have a really big file on a file server with a very poor network connection in between.

try something like this

import time
data = open('ipaddresslist.txt','r')
listofipaddresses = []

#make timming
while True:
    for line in data.readlines():
        listofipaddresses.append(line.strip())
        data.close()
    
    time.sleep(5)

The first part of your question isn't very hard. To load the ip addresses once and use them for the rest of your program

data = open('ipaddresslist.txt','r')
listofipaddresses = []

for line in data.readlines():
    listofipaddresses.append(line.strip())

data.close()

Now for the second part. It sounds like you want to monitor the file for changes and incorporate those changes. The only thing I can come up with is to have a the program repeat the process above every few minutes. Or you could do what you're doing now, I cant believe that the file I/O is that bad. Unless you have a really big file on a file server with a very poor network connection in between.

hi

Thanks for your reply. I did confuse you. The thing is i monitor daily to check the health of the servers. Say by using a cron job. Then I will update the servers.txt with healthy server ip's. Now what i want is to load the servers into a list for the first time after updating the file. For the subsequent requests, it should not open the file even the program is called till the next day. I hope I am clear

That is much more challenging. The list of ip addresses has to be in some file somewhere for python to know what to do. Either it must read the ip addresses from a file when the program is called, or the ip addresses must be hard coded into the python script itself. Either way, python is reading the list from somewhere on the hard drive. The only thing you can really do is move the file I/O from one part of the drive to another, but I don't think that you can avoid it entirely.

Can you try splitting the job into two separate programs? Maybe one script checks servers.txt for the list of healthy ip addresses. That script would be called daily by the cron job. That script could then open another file for writing a write a python script for checking those ip addresses. When the checking script is run it wont have to read from servers.txt because the information would be hard coded into it. I still think it feels like cheating, but that might do what you're asking.

This article has been dead for over six months. Start a new discussion instead.