gevent monkey-patching and breakpoints
Asked Answered
D

5

21

I have been playing with Gevent, and I like it a lot. However I have run into a problem. Breakpoint are not being hit, and debugging doesn't work (using both Visual Studio Python Tools and Eclipse PyDev). This happens after monkey.patch_all() is called.

This is a big problem for me, and unfortunately this is a blocker for the use of gevent. I have found a few threads that seem to indicate that gevent breaks debugging, but I would imagine there is a solution for that.

Does anyone know how to make debugging and breakpoints work with gevent and monkey patching?

Dromous answered 15/7, 2012 at 20:27 Comment(1)
Well, why no use "print" like we are in the 80th? While this in not exactly what are you looking for it may help to fix a bug or two.Zink
G
9

PyCharm IDE solves the problem. It supports gevent code debugging after you set a configuration flag: http://blog.jetbrains.com/pycharm/2012/08/gevent-debug-support/.

Unfortunately, at the moment I don't know a free tool capable of debugging gevent.

UPD: THERE IS! Now there is a community version of PyCharm.

Gorski answered 26/12, 2012 at 10:12 Comment(1)
Because even a year or so later these threads are still useful... Jetbrains now offers a free "community" version of Pycharm -- jetbrains.com/pycharm/download/index.htmlScythia
V
4

pdb - The Python Debugger

import pdb
pdb.set_trace() # Place this where you want to drop into the python interpreter.
Virtu answered 14/8, 2012 at 22:17 Comment(0)
L
4

While debugging in VS Code,

I was getting this error:

It seems that the gevent monkey-patching is being used. Please set an environment variable with: GEVENT_SUPPORT=True to enable gevent support in the debugger.

To do this, in the debug launch.json settings, I set the following:

"env": {
    "GEVENT_SUPPORT": "True"
},
Leer answered 27/10, 2021 at 14:31 Comment(0)
R
1

The simplest, most dangerous solution would be to monkey patch stdin and stdout:

import gevent.monkey
gevent.monkey.patch_all(sys=True)

def my_app():
    # ... some code here

    import pdb
    pdb.set_trace()

    # ... some more code here

my_app()

This is quite dangerous since you risk stdin/stdout behaving in a strange way during the rest of your app lifetime.

Instead you can use a library that I wrote: gtools.pdb. It minimizes the risk to the pdb prompt only:

def my_app():
    # ... some code here

    import gtools.pdb
    gtools.pdb.set_trace()

    # ... some more code here

my_app()

Basically, what it does is tell pdb to use a non-blocking stdin and stdout for its interactive prompt. Any running greenlets will still continue to run in the background.

If you want to avoid the dependency, all you need to do is tell pdb to use a gevent friendly stdin and stdout with something like this:

import sys
from gevent.fileobject import FileObjectThread as File

def Pdb():
    import pdb
    return pdb.Pdb(stdin=File(sys.stdin), stdout=File(sys.stdout))

def my_app():
    # ... some code here

    Pdb().set_trace()

    # ... some more code here

my_app()

Note that with any of these solutions, you loose Key-up, Key-down pdb prompt facilites see gevent issue patching stdin/stdout.

Referee answered 17/3, 2018 at 20:13 Comment(0)
S
0

I use Pycharm 2.7.3 currently and I too was having issues with gevent 0.13.8 breaking debugging. However when I updated to gevent 1.0rc3 I found I could debug again properly.


Sidenote:

I only just now learned that Jetbrains had a workaround with the config flag. I was getting around the problem when I needed to debug with the following hack. I honestly don't know why it worked nor what the negative consequences were. I just did a little trial and error and this happened to allow debugging to work when using grequests.

# overrides the monkeypatch issue which causes debugging in PyDev to not work.
def patch_time():
    return
import gevent.monkey
gevent.monkey.patch_time = patch_time 
import grequests
Scythia answered 1/11, 2013 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.