The following syntax is an example of a virtual import:
from httpout import request
As its name implies, it doesn’t actually import from a file, it just magically retrieves the current request object.
Since httpout uses file-based routing, this is the only viable way I can think of to interact with objects like request
, response
, etc., by leveraging the import syntax.
I am not aware if there are other tools/frameworks that do something like this, so I just call it virtual import. No, it does not rely on threading.local at all!
The objects that can be imported are mostly from the built-in:
from httpout import (
request,
response,
websocket,
print,
run,
wait,
context, # connection-level context
__server__,
__globals__ # worker-level context
)
While __main__ can be imported in this way:
import __main__
Well, to use print, there is no need to do so, unless on other built-ins like run or wait.
It is useful to avoid linter errors:
F821 undefined name 'run'
F821 undefined name '__globals__'
Last but not least, importing httpout
means importing the current module.
import httpout
assert httpout is __server__['modules'][__name__]