eventlet.monkey_patch() breaks ipdb in docker container - python

So I am using the micro-servicing python package nameko which runs a service using eventlet and calls eventlet.monkey_patch() on import.
I have deciphered that it is this piece of code that is blocking any debug attempts via ipdb. The ipdb console shows in the terminal but I cannot type anything and have to close the entire terminal session in order to quit the process.
The stuck console looks like:
How can I use ipdb with this function?
EDIT: This issue only seems to happen when within a docker container.

Sorry, no convenient solution, for now your best option is to skip docker when using ipdb (you can extract filesystem image from docker and run it in another virtualisation, such as qemu, Virtualbox, systemd-nspawn). See https://github.com/larsks/undocker for help.
Other things to try (may not work, please share results):
update eventlet to github master
pip install https://github.com/eventlet/eventlet/archive/master.zip
This issue is cross posted here https://github.com/eventlet/eventlet/issues/361

Related

Pycharm debugger "waiting for connections" but running still working

I am trying to debug django project with docker-compose interpreter.
Here is my pycharm configurations
But when I trying to debug it project still running but debugger is still waiting for connection and break point not working
I think that structure of my project have problem cause i'm try to debug other project it still working.
Here is my project structure
What am i doing wrong?
To whomever else it might help, the problem in my case was that I attempted to use the debugger coupled with a run inside a Docker container functionality.
I also happened to have all ports published on that container which prevented the debugger to connect. Publishing only ports I actually needed, resolved the problem.
Check the running ports on your machine. In my case, the port that PyCharm wanted to use for debugging (127.0.0.1:xxxx) was being used by another program on my laptop.
You can check the running ports using the following command on mac:
lsof -i -P | grep -i "listen"
Or, the following command once you know which port PyCharm is trying to use (usually you can see that on top of the console tab of PyCharm after starting debugging process):
sudo lsof -i :xxxxx
After running that, you should see a list with PID numbers, names of program etc. Then you can kill the running process on that port using the PID:
sudo kill -9 PID
Or, just restart your computer.
If that doesn't work, then it might be due to using already-existing Python module names. Make sure the names of the Python files in your project isn't the same as any other library/code from python.

Pycharm Couldn't connect to console process when using remote docker interpreter

I am trying to run my code within a docker container hosted on an AWS EC2 machine.
It seems that PyCharm can connect to the interpreter because it can show the list of installed packages when looking at the interpreter configuration.
However, when I try to open a Python console, or when I try to run a Python script, I have the error:
3987f6fc2476:/usr/bin/python3 /opt/.pycharm_helpers/pydev/pydevconsole.py --mode=server --port=55516
Couldn't connect to console process.
Process finished with exit code 137 (interrupted by signal 9: SIGKILL)
Happy to provide more information. What is possibly going wrong here? The error seems pretty generic.
EDIT: PyCharm can start the docker container but still the Python console won't work. On the server, docker ps returns:
ecd6a7220b55 9e1ad5b17633 "/usr/bin/python3 /o…" 1 second ago Up Less than a second 22/tcp, 0.0.0.0:50219->50219/tcp dreamy_matsumoto
Turns out that the issue with that PyCharm used a random port every time it starts a Python console when connecting to remote docker container. If we could open all the inbound ports on the EC2, this feature would work. Of course there is nothing worst from a security perspective. Do NOT do this. (but if you really want to do it you'll need to setup docker through TCP)

pycharm and flask autoreload and breakpoints not working

I'm using Pycharm 4, with flask 0.10.1, python 3.4
It seems that when running a flask application from inside pycharm, if I run it with:
app.run(debug=True)
My breakpoints are ignored. After some googling, I've found that in order to make PyCharm stop on breakpoints, I should run flask with:
app.run(debug=True, use_reloader=False)
Now PyCharm correctly stops on breakpoints, but I miss the autoreloading feature.
Is there any way to make both work together?
Using python 2.7 both things work
I reported this to PyCharm: https://youtrack.jetbrains.com/issue/PY-13976
I'm going to start with the short answer: No, what you want cannot be done with any releases of PyCharm up to 4.0.1.
The problem is that when you use the reloader the Flask application runs in a child process, so the PyCharm debugger is attached to the master process and has no control over the child.
The best way to solve this problem, in my opinion, is to ask Jetbrains to build a "restart on change" feature in their IDE. Then you don't need to use Werkzeug's reloader at all and you get the same functionality direct from PyCharm.
Until Jetbrains decides to implement this, I can share my workaround, which is not terribly bad.
In the "Edit Configurations", set the configuration you are going to use to "Single Instance only" (check box in the top right of the dialog box)
Make sure the configuration is the active one.
Configure your Flask app to not use the Werkzeug reloader.
Press Ctrl-D to start debugging (on Mac, others may have a different shortcut)
Breakpoints should work just fine because the reloader isn't active.
Make any code changes you need.
When you are ready to restart, hit Ctrl-D again. The first time you do it you will get a confirmation prompt, something like "stop and restart?". Say yes, and check the "do not show again" checkbox.
Now you can hit Ctrl-D to quickly restart the debugger whenever you need to.
I agree it is not perfect, but once the Ctrl-D gets into your muscle memory you will not even think about it.
Good luck!
I found that in PyCharm 2018.1.2 there is FLASK_DEBUG checbox in run configuration:
With this after making some changes, saving file triggers reload action.
In my setup, I'm debugging the flask app by running a main.py file which sets some configuration and calls app.run(). My python interpreter is set up in a Docker container.
My issue was that I needed to check Run with Python console.
The problem is because with use_reloader=True the werkzeug application is started in a seperate (child) thread of main application and PyCharm fails to correctly handle breakpoints because they are lost when the thread starts.
You can try to follow this thread: http://forum.jetbrains.com/thread/PyCharm-776 but it seams there was not too much progress on that.
I'd suggest using something Python-ish like pdb, i.e.:
#app.route('/<string:page>')
def main(page):
import pdb; pdb.set_trace() # This line actually stops application execution
# and starts Python debug shell in the console
# where you can examine current scope and continue
# normal code execution at any time.
# You can inject *any* code here.
# For example, if you type `print page` during pause,
# it will output content of "page" variable.
return render_template('index.html')
Try configuring this python running configuration in "Edit Configurations". After that, run in debug mode.
You need to unlock the console.
you start the app in debug mode
then you make something that causes an error.
at the end of the error message from flask is this
Here you enter the PIN flask prints in the console at the start
copy paste this pin into the console and click confirm PIN
now the breakpoints will work
from pycharm 2017 using python 2.7 (in my case with virtual env, but I suppose not necessary) I do:
run...
leave scripts and scripts parameters blank
I put in interpreter options: -m flask run
set the env variables FLASK_APP
than run attach to local process, and finally choose the running process
my use case is to connect from postman to flask rest services endpoints and interrupt on my breakpoints

How to debug Django app running on Heroku using a remote pdb connection?

To debug a bug I'm seeing on Heroku but not on my local machine, I'm trying to do step-through debugging.
The typical import pdb; pdb.set_trace() approach doesn't work with Heroku since you don't have access to a console connected to your app, but apparently you can use rpdb, a "remote" version of pdb.
So I've installed rpdb, added import rpdb; rpdb.set_trace() at the appropriate spot. When I make a request that hits the rpdb line, the app hangs as expected and I see the following in my heroku log:
pdb is running on 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc:4444
Ok, so how to connect to the pdb that is running? I've tried heroku run nc 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc 4444 to try to connect to the named host from within heroku's system, but that just immediately exits with status 1 and no error message.
So my specific question is: how do I now connect to this remote pdb?
The general related question is: is this even the right way for this sort of interactive debugging of an app running on Heroku? Is there a better way?
NOTE RE CELERY: Note, I've now also tried a similar approach with Celery, to no avail. The default host celery's rdb (remote pdb wrapper) uses is localhost, which you can't get to when it's Heroku. I've tried using the CELERY_RDB_HOST environment variable to the domain of the website that is being hosted on Heroku, but that gives a "Cannot assign requested address" error. So it's the same basic issue -- how to connect to the remote pdb instance that's running on Heroku?
In answer to your second question, I do it differently depending on the type of error (browser-side, backend, or view). For backend and view testing (unittests), will something like this work for you?
$ heroku run --app=your-app "python manage.py shell --settings=settings.production"
Then debug-away within ipython:
>>> %run -d script_to_run_unittests.py
Even if you aren't running a django app you could just run the debugger as a command line option to ipython so that any python errors will drop you to the debugger:
$ heroku run --app=your-app "ipython --pdb"
Front-end testing is a whole different ballgame where you should look into tools like selenium. I think there's also a "salad" test suite module that makes front end tests easier to write. Writing a test that breaks is the first step in debugging (or so I'm told ;).
If the bug looks simple, you can always do the old "print and run" with something like
import logging
logger = logging.getLogger(__file__)
logger.warn('here be bugs')`
and review your log files with getsentry.com or an equivalent monitoring tool or just:
heroku logs --tail

How to keep web.py app running always?

I am running web py like below from terminal.
$ python app.py 8080
now how do i keep it running even when I close the terminal?
Is there any way to keep this server running.
Thank you
you can use nohup for that.
$ nohup python app.py 8080 &
It will stay open even if you close the terminal. You'll need to use kill and send some signal to it to close it.
I think that this is a question belonging to another stackexchange site (serverfault?).
Anyway, the answer is: run it in a screen:
screen python app.py 8080
Detach from the screen using the key combination:
Ctrl+a+d
Attach to the screen again (to see ouput from your process)
screen -r
Quit screen:
Ctrl+c
You can use supervisord. It is intended to do such tasks (and much more). Documentation is pretty clear, so it should not be a problem to set up it. See how to run supervisord.
Also do not use nohup since it will not do anything like autorestarting server after failure or after reboot.
In a Bourne-shell compatible shell (Bash, Bourne, Korn):
python app.py 8080 &
I just see that you wrote that you want to close the terminal. Apparently my answer won't work then.

Categories

Resources