Skip to content

Instantly share code, notes, and snippets.

@cyberw
Last active May 31, 2019 13:49
Show Gist options
  • Save cyberw/857b9939515ec11ca999cb7f740f7969 to your computer and use it in GitHub Desktop.
Save cyberw/857b9939515ec11ca999cb7f740f7969 to your computer and use it in GitHub Desktop.
Debug locust using VS code (PTVS). Just go to debug, add a debug configuration, select "python file" and run it when standing in the locust script.
if __name__ == "__main__":
with open("enable_gevent_debugging.py", "r") as myfile:
exec(myfile.read()) # pylint: disable=W0122
from locust import HttpLocust, TaskSet, task
class WebTaskSet(TaskSet):
@task
def asdf(self):
self.client.post("/myurl")
# just do one iteration
if __name__ == "__main__":
exit(0)
class DebuggingLocust(HttpLocust):
task_set = WebTaskSet
host = "http://myserver"
# if someone runs just this script (not using locust -f ...), just spawn a single locust directly
if __name__ == "__main__":
x = DebuggingLocust()
x.run()
# Gevent hangs during monkey patching when a debugger is attached
# Source this file before importing locust when you want to use a debugger
# Original code by ayzerar at https://github.com/Microsoft/PTVS/issues/2390
from gevent import monkey
monkey.patch_all()
def setup_ptvsd(host="0.0.0.0", port=5678):
import sys
saved_modules = {}
try:
green_modules = set(
[
"socket",
"ssl",
"select",
"urllib",
"thread",
"threading",
"time",
"logging",
"os",
"signal",
"subprocess",
"requests",
]
)
for modname in list(sys.modules.keys()):
if modname.partition(".")[0] in green_modules:
saved_modules[modname] = sys.modules.pop(modname)
import ptvsd # pylint: disable=W0611
ptvsd.enable_attach(address=(host, port), redirect_output=False)
finally:
sys.modules.update(saved_modules)
setup_ptvsd()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment