2

So I have a process where I sunbmit a series of directories and files to a service. The service processes those files and then returns the directories and output files into a different directory

For example, there will be a parent directory with the run name and then subdirectories for each date we are processing.. i.e.:

Input

RunName

    2020-06-01
    2020-06-02
    2020-06-03

The service creates an equivalent directory under output for the run name and then the same directories as they process the data , so when it processes the fist file, it looks like this:

Output

RunName

    2020-06-01
    
    

When the service is done processing all files, it puts a "done" file in the direcotry with all of the dates. Once the donefile is received, my program can move on to the next stage of processing.

What I am trying to do is to determine, what percentage of the processing is done and then, when it is complete.

The code below works flawlessly, except for one thing

I constantly get

ERROR:paramiko.transport:Socket exception: An existing connection was forcibly closed by the remote host (10054) Pointing to the 2nd line (that starts with "While")

What changes do I need to make to force a reconnect and then try again when this error happens and let the while loop continue along its' metty way.

Any help will be greatly appreciated.. this kills me because it interrupts a much larger process and causes a backlog.

srv = pysftp.Connection(host=servername, username=user,password=pwd,cnopts=cnopts)

while srv.isfile(donefile)==False:
    try:
        srv = pysftp.Connection(host=servername, username=user,password=pwd,cnopts=cnopts)
        dirnames=[]
        for i in srv.listdir(outputdir):
            if i[:6]=="date=2":
                dirnames.append(i)


        srv.close()
        if max(dirnames)!=lastname:
            print("Last Directory is: " ,max(dirnames), "out of", max(dirnamesin), " ",round((len(dirnames)/len(dirnamesin))*100), "pct done", datetime.datetime.now(), "run: ",run_name)
        lastname=max(dirnames)
        time.sleep(30)
        srv = pysftp.Connection(host=servername, username=user,password=pwd,cnopts=cnopts)
    except:
        print("connection error - will try again in 30 seconds")
        time.sleep(30)
        srv = pysftp.Connection(host=servername, username=user,password=pwd,cnopts=cnopts)
print ('done')

1 Answers1

1

I would try to refactor this to ensure that connections are properly closed, something like

while True:
    try:
        with pysftp.Connection(host=servername, username=user,password=pwd,cnopts=cnopts) as srv:
            if srv.isfile(donefile):
                break
            dirnames=[]
            for i in srv.listdir(outputdir):
                if i[:6]=="date=2":
                    dirnames.append(i)
        if max(dirnames)!=lastname:
            print("Last Directory is: " ,max(dirnames), "out of", max(dirnamesin), " ",round((len(dirnames)/len(dirnamesin))*100), "pct done", datetime.datetime.now(), "run: ",run_name)
        lastname=max(dirnames)
    except Exception:
        print("connection error - will try again in 30 seconds")
    time.sleep(30)
print('done')
Gribouillis
  • 2,230
  • 1
  • 9
  • 14