Can't connect to the flow when using as library

Sorry, I should have been more clear. To debug mitmdump (or mitmproxy/mitmweb) in PyCharm, you have to pick venv/bin/mitmdump-script.py (venv/Scripts/mitmdump-script.py on Windows) as the file to run/debug.

Mitmproxy’s functionality is a bit limited when it comes to simulating lagging (you have to use the concurrent decorator for that, and that’s more of a workaround), but other than that all those should be possible.

You can also run mitmproxy in a separate thread of your application. https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/tools/main.py should give you some hints on how to do that.