How to stop one scheduler iteration in Python
Question: I am new to the Python (and programming in general). I have a pcode that
periodically downloads the website content through PyCURL and do some
searching. I have used scheduler inside the While-True to set up an infinite
loop where I create an object and call its method start()to grab the website
and perform some searchs.. Problem occurs when getbody() methon can't fetch
the website, due to the connection problems (or another reasons).
BeautifulSoup expects the string, raises an Error otherwise.

How to stop a Thread of the scheduler programme when an Error/Exception occurs
right in the getbody() method and so wait for another Thread? Returning an
empty string as a result of getbody() method is a waste of cpu time.

    
    
    #Parser_module
    class Parser(object):
        def __init__(self):
            self.body = BeautifulSoup(self.getbody(), "lxml")
            self.buffer = BytesIO()
    
        def getbody(self):
            # some code to set pycurl up
            try:
                c.perform()
            except pycurl.error:
                print("connection error")
                # returns an emptry string to feed the BeautifulSoup with
                return ""
            body = self.buffer.getvalue().decode("utf-8")
            return body
    
         def start(self):
             #calls other functions to perform some searching
             self.otherfunction()
    
         def otherfunction(self):
             .
             .
             .
    
    #Scheduler module
    import Parser_module
    from threading import Timer
    
    def start_search():
        parser = Parser()
        parser.start()
        t = Timer(20.0, start_search)
        t.start()
    


Answer: Instead of fetching the URL in the `Parser.__init__`, you could just do that
in `Parser.start` and return if an error occurs.

    
    
    class Parser(object):
        def __init__(self):
            self.body = None
            self.buffer = BytesIO()
    
        def start():
            data = self.getbody()
            if not data:
                return
            self.body = BeautifulSoup(data, "lxml")
            self.otherfunction()
    
        def getbody(self):
            ...
    
        def otherfunction(self):
            ...
    

On a side note, I suggest that you use the much nicer
[requests](http://docs.python-requests.org/en/latest/) library instead of
pycurl, if you can. Also check out
[PEP8](https://www.python.org/dev/peps/pep-0008/), the Python Style Guide,
e.g. for some suggestions on how to name things.

