0

I'm using the following lib for creating ftp server called pyftpdlib.
Following the exact example of building a base ftp server apart of the fact that I've created my own custom handler and passing it as in the example.

First question: I need to be able to poll every 50 seconds on a static dictionary (class variable) defined in the custom handler. After each interval of 50 seconds I need to check for some condition and act accordingly.

As you probably see ,the custom handler is being used by the FTP server, I guess I need some working thread that will be able poll on the custom handler's static dictionary every 50 seconds, how do I launch another thread that will be bound to the custom handler and do that kind of timer polling?

Second question: Is there some way to know that a time of 50 seconds has been passed (on the second it passed) since a specific timestamp which was read from the static dict - I mean to do that without polling every X seconds to check if time has passed, which can be inaccurate.

JavaSa
  • 5,813
  • 16
  • 71
  • 121

1 Answers1

0

First Question

In your worker thread, use the class based version of creating a thread. You will pass your handler to the constructor of object and that object will have a reference to the same custom handler you already created.

In my example, "handler" should be wrapped in a function with a mutex to make it thread safe

Please note that you may want to add a mutex to the custom handler to prevent data races and to ensure that this is thread safe. The code is just a sample to give you an idea. If it doesn't work as-is. just let me know.

from threading import Thread
from time import sleep

class Worker(Thread):
    def __init__(self, custom_handler):
        self.handler = custom_handler

    def run(self):
        while(True):
            sleep(50)

            # check handler
            if self.handler:
                # do something
                pass



handler = {}
worker = Worker(handler)

# start thread
worker.run()

Second Question

Not sure what you mean by "know" that 50 seconds have elapsed. These long duration pollings are typically done by making the task sleep for a given amount of time and then waking up to do work. Does this method not suit your requirements? But you are right, you may still experience some drift on your time, but is that an absolute requirement?

Dan
  • 1,096
  • 9
  • 13