tags:

views:

18

answers:

1

This scripts checks an FTP and download files at scheduled intervals.

Sending to it the right signal (SIGUSR1) should make it close gracefully, waiting checkAll to complete, if running.

class ScheduledFtpCheck(FtpCheck, Scheduler):
    def __init__(self):
         ...
         self.aborted, self.checking = False, False

    def abort(self, *args, **kwargs):
        self.aborted = True
        if not self.checking: self.quit()

    def checkAll(self):
        if not self.isAborted():
            self.checking = True
            self.checkAll()
            self.checking = False
            if self.aborted: self.quit()

    ...

def main():
    sch = ScheduledFtpCheck()
    signal.signal(signal.SIGUSR1, sch.abort)
    sch.start()

the problem is I get a Interrupted system call that makes ftplib.FTP exit prematurely:

Traceback (most recent call last):
  File "/root/my-applications/sensor/sensor/lib/importutils2.py", line 137, in connectAndCheckAll
    try: self.checkAll()
  ...
  File "/usr/lib/python2.6/ftplib.py", line 182, in getline
    line = self.file.readline()
  File "/usr/lib/python2.6/socket.py", line 406, in readline
    data = self._sock.recv(self._rbufsize)
error: [Errno 4] Interrupted system call

So my script handles signal correctly but scheduled check don't.

Why? How can I prevent it?

Thanks for your support

A: 

I found myself the solution:

running ftp process in a separate thread isolated it from main process signals

threading.Thread(target=self.checkAll).start()
neurino