Created
April 25, 2020 15:56
-
-
Save marnitto/72ac338cf591c050b81a51c0bc60d70a to your computer and use it in GitHub Desktop.
Zappa: get 'context' variable from asynchronous task function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import inspect | |
def get_context(): | |
# *crawl* hidden context variable from stack frame | |
for fi in inspect.getouterframes(inspect.currentframe()): | |
local = fi.frame.f_locals | |
context = local.get('context') | |
if 'LambdaContext' in str(type(context)): | |
return context | |
@task | |
def test_task(): | |
context = get_context() | |
log.info('context=%r' % (context,)) # <bootstrap.LambdaContext object at 0x7f9a0586b250> | |
return True | |
def test(): | |
test_task() | |
return True | |
''' | |
Tested version: py3.7 | |
To manually test: | |
1. zappa deploy (or zappa update) | |
2. zappa invoke app.test | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment