mirror of
https://github.com/vee1e/mitmproxy.git
synced 2026-09-01 18:27:18 +00:00
Support async hooks. Fixes #4207.
This commit is contained in:
parent
8c86fd06db
commit
cee4b72459
3 changed files with 55 additions and 11 deletions
|
|
@ -1,16 +1,26 @@
|
|||
"""
|
||||
Make events hooks non-blocking.
|
||||
|
||||
When event hooks are decorated with @concurrent, they will be run in their own thread, freeing the main event loop.
|
||||
Please note that this generally opens the door to race conditions and decreases performance if not required.
|
||||
Make events hooks non-blocking using async or @concurrent
|
||||
"""
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
from mitmproxy.script import concurrent
|
||||
from mitmproxy import ctx
|
||||
|
||||
|
||||
@concurrent # Remove this and see what happens
|
||||
def request(flow):
|
||||
# Hooks can be async, which allows the hook to call async functions and perform async I/O
|
||||
# without blocking other requests. This is generally preferred for new addons.
|
||||
async def request(flow):
|
||||
ctx.log.info(f"handle request: {flow.request.host}{flow.request.path}")
|
||||
await asyncio.sleep(5)
|
||||
ctx.log.info(f"start request: {flow.request.host}{flow.request.path}")
|
||||
|
||||
|
||||
# Another option is to use @concurrent, which launches the hook in its own thread.
|
||||
# Please note that this generally opens the door to race conditions and decreases performance if not required.
|
||||
# Rename the function below to request(flow) to try it out.
|
||||
@concurrent # Remove this to make it synchronous and see what happens
|
||||
def request_concurrent(flow):
|
||||
# This is ugly in mitmproxy's UI, but you don't want to use mitmproxy.ctx.log from a different thread.
|
||||
print(f"handle request: {flow.request.host}{flow.request.path}")
|
||||
time.sleep(5)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue