Search code examples
pythonpython-3.6python-multithreadinglong-polling

Is there a recommeded way to debug the cause of a freezing web application's backend in Python?


My backend is written in Python, using CherryPy. There is a section of my code handling long-pollings from the front end, as illustrated below:

# for the long-polls
longpoll_blockers_channel = {}

class MyApp:
...
    @cherrypy.expose
    @uses_json
    def register_longpoll_blocker(self, classmodel_name):
        if classmodel_name not in longpoll_blockers_channel:
            unblock_event_obj = threading.Event()
            unblock_event_obj.running_status = "long-polling"
            longpoll_blockers_channel[classmodel_name] = unblock_event_obj

        def unblocking_event_handler(mapper, connection, target):
            longpoll_blockers_channel[classmodel_name].set()

        listento_classmodel_obj = database.schemas_registry[classmodel_name]
        # triggered by trio events:
        for data_changing_evt in [
                'after_delete', 'after_insert', 'after_update'
        ]:
            sqlalchemy.event.listen(listento_classmodel_obj, data_changing_evt,
                                    unblocking_event_handler)
        return f"Successfully registered a blocker at backend for {classmodel_name}!"

I noticed that the above code seems to be only handle up to 4 long-polling registered threads. After reaching 4 or more, the backend freezes. I suspect that the freezing is caused by too many threads spawned by threading.Event(), but can't prove it. I want to find out exactly what is causing the backend to hang.

Is having many threading.Event() threads likely to freeze the app? How to debug a bug like this?


Solution

  • After a few days research I found out this idling is due to the browser's blocking mechanism, which only allows a certain number of parallel connection at the same time. It has NOTHING to do with Python's backend and threading.