Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm breakpoints on Thread for Google App Engine

PyCharm Professional debugger has one issue when working with Google App Engine. Breakpoints do not work when debugging Threads launched manually by my code.
This affects ability to debug code running on dev_appserver which uses threading.Thread or concurrent.futures.ThreadPoolExecutor 2.7 backport (which is officially supported by GAE now)

The issue occurs both in PyCharm 2017.2 and 2017.3.4. Observed on GAE SDK 1.9.66 on Ubuntu Linux.

Here is a repro code - call this from any request handler.

from concurrent.futures import ThreadPoolExecutor, wait
from threading import Thread
def worker():
    logging.info("Worker")  # set breakpoint here
    time.sleep(3)

def call_this():  # call this from your request handler
    tpe = ThreadPoolExecutor(max_workers=5)
    futures = [tpe.submit(worker) for i in range(10)]
    wait(futures)
    threads = [Thread(worker) for i in range(10)]
    for t in threads: t.start()
    for t in threads: t.join()
like image 275
Alek Kowalczyk Avatar asked Mar 17 '26 02:03

Alek Kowalczyk


1 Answers

Quick fix is to patch patch_threads function in <pycharm-folder>/helpers/pydev/pydevd.py around line 978 and add settrace for the separate GAE's module:

 def patch_threads(self):
    try:
        # not available in jython!
        import threading
        threading.settrace(self.trace_dispatch)  # for all future threads
        from google.appengine.dist27 import threading as gae_threading
        gae_threading.settrace(self.trace_dispatch)  # for all future threads
    except Exception as e:
        pass

    from _pydev_bundle.pydev_monkey import patch_thread_modules
    patch_thread_modules()
like image 171
Alek Kowalczyk Avatar answered Mar 19 '26 16:03

Alek Kowalczyk