{"id":312573,"date":"2020-11-05T09:00:20","date_gmt":"2020-11-05T09:00:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=312573"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=312573","title":{"rendered":"\u0417\u0430\u0431\u044b\u0432\u0430\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e? \u0412\u0430\u043c \u043f\u043e\u043c\u043e\u0436\u0435\u0442 contextvars"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ve\/yy\/dv\/veyydvnoed0iszivpdazjypyets.png\"><\/p>\n<p>  <\/p>\n<p>\u041c\u044b \u0432 \u042f\u043d\u0434\u0435\u043a\u0441.\u0422\u0430\u043a\u0441\u0438 \u043b\u044e\u0431\u0438\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043b\u043e\u0433\u0438. \u0415\u0449\u0451 \u0431\u043e\u043b\u044c\u0448\u0435 \u043c\u044b \u043b\u044e\u0431\u0438\u043c, \u043a\u043e\u0433\u0434\u0430 \u043b\u043e\u0433\u0438 \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u043d\u0430\u043c \u0440\u0430\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435. \u041f\u0440\u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0432 \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0442\u044b\u0441\u044f\u0447 RPS \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u043b\u043e\u0433-\u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u043c\u0430\u043b\u043e. \u0425\u043e\u0447\u0435\u0442\u0441\u044f \u0443\u043c\u0435\u0442\u044c \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u043e\u0433\u0438 \u043f\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e, \u0432\u0438\u0434\u0435\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0433\u043e API, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u0433\u043b\u0443\u0431\u043b\u044f\u0442\u044c\u0441\u044f \u0432 \u043b\u043e\u0433\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0430\u043a\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043a\u0430\u0436\u0434\u0430\u044f \u043b\u043e\u0433-\u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435 \u0441\u043e\u043f\u0440\u043e\u0432\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439: id \u0437\u0430\u043a\u0430\u0437\u0430, \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0437\u0430\u0431\u044b\u0432\u0430\u044e\u0442 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043f\u0440\u0438 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/lw\/oq\/-c\/lwoq-cxf1mvzg0vxm4obrr3mkiy.png\"><br \/>  <em>\u0418\u0437 \u043b\u043e\u0433\u043e\u0432 \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u0448\u043b\u043e \u043d\u0435 \u0442\u0430\u043a<\/em><\/p>\n<p>  <\/p>\n<h2 id=\"problema\">\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/h2>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0435\u0431\u0435 \u0442\u0430\u043a\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">@route('\/feedback') async def feedback_handler(request):     log_extra = {         'handler': 'some-handler',         'user_id': request.user_id,     }     ...     order = await db.feedbacks.find_one(order_id=request.order_id)     if order is None:         logger.warning(f'user has no active orders in db', extra=log_extra)         return process_without_order(request)  # output: # text=&quot;user has no active orders in db&quot; handler=some-handler user_id=some-guid<\/code><\/pre>\n<p>  <\/p>\n<p>\u0425\u0435\u043d\u0434\u043b\u0435\u0440 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 <code>POST \/feedback?order_id=d34db33f<\/code> \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 \u0432 query-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0435 \u0437\u0430\u043a\u0430\u0437, \u0435\u0441\u043b\u0438 \u0437\u0430\u043a\u0430\u0437\u0430 \u0432 \u0411\u0414 \u043d\u0435\u0442 \u2014 \u0440\u0430\u043f\u043e\u0440\u0442\u0443\u0435\u0442 \u043e\u0431 \u044d\u0442\u043e\u043c \u0432 \u043b\u043e\u0433\u0435. \u0412\u044b\u0437\u043e\u0432 \u043b\u043e\u0433\u0435\u0440\u0430 \u0441\u043e\u043f\u0440\u043e\u0432\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u044f\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 <code>extra<\/code>. \u0412 \u043d\u0451\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u043c \u043b\u043e\u0433\u0430. \u0423 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442 <code>extra<\/code> \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439-\u043b\u043e\u0433\u0435\u0440\u043e\u0432.<\/li>\n<li>\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 <code>extra<\/code> \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0442\u0435\u0441\u0442\u043e\u0432 (\u0442\u0435\u0441\u0442\u044b \u043d\u0430 \u043b\u043e\u0433\u0438 \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u043f\u0438\u0448\u0435\u043c).<\/li>\n<li>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 <code>extra<\/code> \u043b\u043e\u0432\u044f\u0442\u0441\u044f \u043d\u0430 \u0440\u0435\u0432\u044c\u044e \u0447\u0430\u0441\u0442\u043e, \u043d\u043e \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430.<\/li>\n<li>\u041e\u0434\u043d\u0430\u0436\u0434\u044b \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 <code>extra<\/code> \u043d\u0443\u0436\u043d\u043e \u0442\u0430\u0449\u0438\u0442\u044c \u0441\u043a\u0432\u043e\u0437\u044c \u0432\u0441\u0435 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u044b, \u0445\u043e\u0442\u044f \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u043d \u043d\u0435 \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u043b\u043e\u0433\u0438\u043a\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/li>\n<\/ul>\n<p>  <\/p>\n<pre><code class=\"python\"># handler.py async def handler(request):     log_extra = derive_extra(request)     # ...     await foo.some_logic1(arg, kwarg=kwarg, log_extra=log_extra)     # ...  # foo.py async def some_logic1(..., log_extra=None):     # ...     await bar.some_logic2(..., log_extra=log_extra)     # ...  # bar.py async def some_logic2(..., log_extra=None):     # ...     await qux.some_logic3(..., log_extra=log_extra)     # ...  # qux.py async def some_logic3(..., log_extra=None):     # ...     if something:         logger.warning('Something has happened', extra=log_extra)     # ...<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u0437\u0430\u0431\u044b\u0432\u0430\u044e\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043a \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u043e\u0432 \u0442\u044f\u0436\u0435\u043b\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u043b\u043e\u0433\u043e\u0432.<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/4d\/rs\/nm\/4drsnmehfc6ncbn9ivamz_yal5g.png\"><br \/>  <em>\u0418\u0437 \u043b\u043e\u0433\u043e\u0432 \u043d\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u043f\u0440\u0438\u0447\u0438\u043d\u0443 404 \u043a\u043e\u0434\u0430<\/em><\/p>\n<p>  <\/p>\n<h2 id=\"resheniya\">\u0420\u0435\u0448\u0435\u043d\u0438\u044f<\/h2>\n<p>  <\/p>\n<h3 id=\"propatchit-loger\">\u041f\u0440\u043e\u043f\u0430\u0442\u0447\u0438\u0442\u044c \u043b\u043e\u0433\u0435\u0440<\/h3>\n<p>  <\/p>\n<p>\u0417\u0430\u0434\u0430\u0434\u0438\u043c\u0441\u044f \u0446\u0435\u043b\u044c\u044e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>extra<\/code> \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0434\u043b\u044f \u043b\u043e\u0433\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0442\u044c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043b\u043e\u0433\u0435\u0440\u0430 \u0434\u043b\u044f \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>logging.getLogger()<\/code>. \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u044d\u0442\u0438\u043c \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u043b\u0430\u0441\u0441, \u0434\u043b\u044f \u043b\u043e\u0433\u0438\u043d\u0433-\u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>extra<\/code> \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c. <\/p>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\">import logging  class MyLogger(logging.getLoggerClass()):     sentinel = object()      def __init__(self, name):         logging.Logger.__init__(self, name=name)      def _log(self, *args, extra=sentinel, **kwargs):         if extra == self.sentinel:             raise ValueError('Extra missed in logger call')         super()._log(*args, **kwargs)  logging.setLoggerClass(MyLogger) logger = logging.getLogger(__name__) logger.error('With extra', extra={'meta': 'data'}) logger.error('Explicit no extra', extra=None) logger.error('Error message without extra')  # output: # With extra # Explicit no extra # Traceback (most recent call last): # ... # ValueError: Extra missed in logger call<\/code><\/pre>\n<\/div><\/div>\n<p>  <\/p>\n<p>\u041f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0442\u0435\u0441\u0442\u043e\u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0430\u0434\u0430\u0442\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043b\u043e\u0433\u0435\u0440\u0430 \u0431\u0435\u0437 <code>extra<\/code>.<\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u043f\u0430\u0441\u043d\u043e. \u0415\u0441\u043b\u0438 \u043c\u044b \u043d\u0435 \u043f\u043e\u043a\u0440\u043e\u0435\u043c \u0442\u0435\u0441\u0442\u0430\u043c\u0438 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u0447\u043a\u0438 \u0441 \u043b\u043e\u0433\u0438\u043d\u0433\u043e\u043c, \u043c\u044b \u0440\u0438\u0441\u043a\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0432 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435. \u0414\u0430, \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u043e \u043f\u043e\u043f\u0430\u0434\u0430\u0442\u044c \u0432 \u043d\u0435\u043f\u043e\u043a\u0440\u044b\u0442\u0443\u044e \u0442\u0435\u0441\u0442\u0430\u043c\u0438 \u0441\u0442\u0440\u043e\u0447\u043a\u0443 \u043d\u0430 \u0431\u043e\u044e, \u043e\u0434\u043d\u0430\u043a\u043e \u0432\u0440\u044f\u0434 \u043b\u0438 \u0431\u0438\u0437\u043d\u0435\u0441 \u0441\u043e\u0433\u043b\u0430\u0441\u0435\u043d \u043f\u044f\u0442\u0438\u0441\u043e\u0442\u0438\u0442\u044c \u043f\u0440\u0438 \u043d\u0435\u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u043c <code>extra<\/code>. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0430\u0437\u0432\u0438\u0442\u044c \u0438\u0434\u0435\u044e \u0444\u043e\u0440\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043b\u0435\u0435.<\/p>\n<p>  <\/p>\n<h3 id=\"staticheskiy-analiz\">\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0430\u043d\u0430\u043b\u0438\u0437<\/h3>\n<p>  <\/p>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 <code>extra<\/code> \u0432 \u0432\u044b\u0437\u043e\u0432\u0430\u0445 \u043b\u043e\u0433\u0435\u0440\u0430 \u0434\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u044f\u0441\u044c \u043d\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043a\u043e\u0434\u0435. \u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0435\u0434\u0432\u0430 \u043b\u0438 \u043d\u0435 \u0433\u0440\u0435\u043f\u043e\u043c, \u043d\u043e \u0440\u0430\u0437 \u0443\u0436 \u043c\u044b \u043f\u0438\u0448\u0435\u043c \u043d\u0430 \u043f\u0438\u0442\u043e\u043d\u0435, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u0435\u043c <a href=\"https:\/\/docs.python.org\/3\/library\/ast.html\">ast<\/a>. \u0417\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0438\u043d\u0443\u0442 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430\u043a\u043e\u043b\u0435\u043d\u043e\u0447\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442, \u0438 \u043e\u043d \u0432 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442 \u0432\u0441\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>logger<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043d\u0435 \u0431\u044b\u043b \u043f\u0435\u0440\u0435\u0434\u0430\u043d \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>extra<\/code>. <\/p>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\"># linter.py import ast, sys code = open(sys.argv[1]).read() for node in ast.walk(ast.parse(code)):     # remember: not production-ready, just proof of concept     if isinstance(node, ast.Call) and not isinstance(node.func, ast.Name):         is_logger = node.func.value.id == 'logger'         if is_logger and 'extra' not in [k.arg for k in node.keywords]:             print(f'Missing extra at line {node.lineno}')  # lint_example.py def foobar():     log_extra = {'meta': 'data'}     bar()     logger.warning('Has no extra')     baz()     logger.warning('Has extra', extra=log_extra)     logger.warning('No extra again')  # $ python3.7 linter.py lint_example.py # Missing extra at line 4 # Missing extra at line 7<\/code><\/pre>\n<\/div><\/div>\n<p>  <\/p>\n<p>\u0412\u043b\u043e\u0436\u0438\u0432 \u0432 \u043d\u0435\u0433\u043e \u0435\u0449\u0451 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u043e\u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c\u043e\u0435 \u0434\u043b\u044f \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u0432 CI \u0440\u0435\u0448\u0435\u043d\u0438\u0435. \u041e\u0434\u043d\u0430\u043a\u043e \u0443 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0430. \u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u043b\u043e\u0433-\u0437\u0430\u043f\u0438\u0441\u0438 \u0431\u0435\u0437 \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043b\u043e\u0433\u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 \u0434\u043e \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430). \u0414\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443 \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u044f\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c <code>extra=None<\/code>, \u043b\u0438\u0431\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c ignore-\u0441\u0442\u0440\u043e\u043a\u0443. \u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0432\u0441\u0451 \u0435\u0449\u0451 \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u044b \u0442\u0430\u0449\u0438\u0442\u044c <code>log_extra<\/code> \u0441\u043a\u0432\u043e\u0437\u044c \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u044b \u0432\u0441\u0435\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432.<\/p>\n<p>  <\/p>\n<p>\u0418\u0442\u043e\u0433: \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u043b\u0438\u043d\u0442\u0435\u0440\u043e\u043c \u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c, \u043d\u043e \u043d\u0435 \u0438\u0434\u0435\u0430\u043b\u0435\u043d.<\/p>\n<p>  <\/p>\n<h2 id=\"avtomaticheskoe-dobavlenie-extra\">\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 extra<\/h2>\n<p>  <\/p>\n<p>\u041e\u0431\u0441\u0443\u0434\u0438\u0432 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043c\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043a \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e, \u0441\u043d\u044f\u0432 \u044d\u0442\u0443 \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u044c \u0441 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a\u0438\u0435 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a \u043b\u043e\u0433\u0435\u0440\u0443 \u0432\u044b\u0437\u043e\u0432\u0430\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>log<\/code>, \u043e\u0431\u0451\u0440\u0442\u043a\u0438 \u043d\u0430\u0434 <code>print<\/code>.<\/p>\n<p>  <\/p>\n<h3 id=\"sinhronnoe-odnopotochnoe-prilozhenie\">\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0435 \u043e\u0434\u043d\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/h3>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443, \u2014 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f, \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0432 \u043d\u0435\u0451 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438 \u0434\u043e\u0441\u0442\u0430\u0451\u043c \u0435\u0433\u043e \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0447\u0442\u043e-\u0442\u043e \u0437\u0430\u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">LOG_EXTRA = {}  def set_log_extra(extra):     global LOG_EXTRA     LOG_EXTRA = extra  def get_log_extra():     return LOG_EXTRA  def log(text):     extra_tskv = '\\t'.join(f'{k}={v}' for k, v in get_log_extra().items())     msg = f'text=&quot;{text}&quot;\\t{extra_tskv}'     print(msg)  @app.route('\/feedback') def feedback_handler_sync_singlethreaded(request):     set_log_extra({ ... })     order = db.feedbacks.find_one(order_id=request.order_id)     if order is None:         log('user has no active orders in db')         return process_without_order(request)  # output: # text=&quot;user has no active orders in db&quot; handler=some-handler user_id=some-guid<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0432\u043e\u0440\u043a\u0435\u0440 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435. \u041e\u0434\u043d\u0430\u043a\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440\u044b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0432 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435, \u0430 \u0434\u043b\u044f \u043d\u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0438\u0437-\u0437\u0430 \u0433\u043e\u043d\u043e\u043a.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<ol>\n<li>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u2116 1, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>set_log_extra<\/code> \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0432 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>  <\/li>\n<li>\n<p>\u041d\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0437\u0430\u043a\u0430\u0437 \u0432 \u0431\u0430\u0437\u0435 \u2014 \u043d\u0430\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u0432\u0440\u0435\u043c\u044f \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<p>  <\/li>\n<li>\n<p>\u0412 \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0430\u0447\u0430\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u2116 2 \u0438 \u0432\u044b\u0437\u0432\u0430\u043b <code>set_log_extra<\/code> \u0441\u043e \u0441\u0432\u043e\u0438\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c.<\/p>\n<p>  <\/li>\n<li>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u0430\u043c\u044f\u0442\u044c \u0443 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043e\u0431\u0449\u0430\u044f, <code>set_log_extra<\/code> \u043e\u0431\u043d\u043e\u0432\u0438\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0442\u0440\u0435\u0434\u043e\u0432.<\/p>\n<p>  <\/li>\n<li>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u2116 1 \u0437\u0430\u043b\u043e\u0433\u0438\u0440\u0443\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0438\u0437 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u2116 2.<\/p>\n<p>  <\/li>\n<\/ol>\n<p>  <\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u00ab\u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c\u00bb \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0445 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432.<\/p>\n<\/div><\/div>\n<p>  <\/p>\n<h3 id=\"mnogopotochnoe-prilozhenie\">\u041c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/h3>\n<p>  <\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0441 \u0433\u043e\u043d\u043a\u0430\u043c\u0438 \u0438\u043c\u0435\u0435\u0442 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435: \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043d\u0435 \u0432 \u043e\u0431\u0449\u0435\u0439 \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0430 \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0440\u0435\u0434\u0430. \u0412 UNIX \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c <a href=\"https:\/\/en.wikipedia.org\/wiki\/Thread-local_storage\">thread-local storage<\/a>, \u0430 Python \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u0443\u044e \u043f\u043e \u0441\u043c\u044b\u0441\u043b\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/docs.python.org\/3\/library\/threading.html#thread-local-data\">threading.local<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">import threading  LOG_EXTRA = threading.local() LOG_EXTRA.data = {}  def set_log_extra(extra):     global LOG_EXTRA     LOG_EXTRA.data = extra  def get_log_extra():     return LOG_EXTRA.data  # ...<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u043e \u0441\u0432\u043e\u0438\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u043e\u043b\u044f <code>LOG_EXTRA.data<\/code>.<\/p>\n<p>  <\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u043d\u043e\u0432\u044b\u0445 \u0431\u044d\u043a\u0435\u043d\u0434\u043e\u0432 \u043f\u0438\u0448\u0443\u0442\u0441\u044f \u043d\u0430 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430\u0445 \u0432\u0440\u043e\u0434\u0435 <code>aiohttp<\/code>. \u0412 \u043c\u0438\u0440\u0435 event-loop\u2019\u043e\u0432 \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445, \u043d\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0434\u043b\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 <code>threading.local<\/code> \u043d\u0435 \u0440\u0435\u0448\u0438\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443: \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043c\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0435\u0434\u0438\u043d\u0443\u044e\u00bb \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e.<\/p>\n<p>  <\/p>\n<h3 id=\"asinhronnoe-prilozhenie\">\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/h3>\n<p>  <\/p>\n<p>\u0410 \u0435\u0441\u043b\u0438 \u0431\u044b \u0443 \u043d\u0430\u0441 \u0431\u044b\u043b \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0430\u043d\u0430\u043b\u043e\u0433 <code>threading.local<\/code>, \u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0435\u0447\u0442\u043e, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u00ab\u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u043e\u0439 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435\u00bb \u0437\u0430\u0434\u0430\u0447\u0438. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 <code>asyncio<\/code> \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0430\u043d\u0430\u043b\u043e\u0433\u043e\u043c \u0442\u0440\u0435\u0434\u043e\u0432 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-task.html#asyncio.Task\">\u0442\u0430\u0441\u043a\u0438<\/a> \u2014 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u044b <code>asyncio.Task<\/code>, \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0438\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043f\u043e\u0432\u0435\u0440\u0445 event-loop\u2019\u0430.<\/p>\n<p>  <\/p>\n<p>\u0412 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u043c \u043d\u0430\u043c\u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435 <code>aiohttp<\/code> \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0440\u0443\u0442\u0438\u043d, \u043d\u043e \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0432\u0441\u0435 \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0439 <code>asyncio.Task<\/code> (\u0435\u0441\u043b\u0438 \u043c\u044b, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043d\u0435 \u043f\u043e\u0440\u043e\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0435 \u0442\u0430\u0441\u043a\u0438 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e).<\/p>\n<p>  <\/p>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u044d\u0442\u0438\u043c \u0444\u0430\u043a\u0442\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f: <code>asyncio<\/code> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-task.html#asyncio.current_task\">\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0442\u0430\u0441\u043a\u0438<\/a> \u0447\u0435\u0440\u0435\u0437 <code>asyncio.current_task<\/code>, \u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u043d\u0435\u0439 \u0447\u0435\u0440\u0435\u0437 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">import asyncio _log_extra_key = '_log_extra'  def set_log_extra(log_extra, task=None):     if task is None:         task = asyncio.current_task()     setattr(task, _log_extra_key, log_extra)  def get_log_extra(task=None):     if task is None:         task = asyncio.current_task()     return getattr(task, _log_extra_key, None)  def log(text):     extra_tskv = '\\t'.join(f'{k}={v}' for k, v in get_log_extra().items())     msg = f'text=&quot;&quot;{text}&quot;&quot;\\t{extra_tskv}'     print(msg)  async def feedback_handler_example():     set_log_extra({'handler': 'some-handler', 'user_id': 'some-guid'})     log(f'no explicit extra')  asyncio.run(feedback_handler_async())  # output: # text=&quot;no explicit extra&quot; handler=some-handler user_id=some-guid<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0435\u0449\u0451 \u043e\u0434\u043d\u0443 \u0442\u0430\u0441\u043a\u0443. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0434\u0432\u0430 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\"># ...  async def call_foo():     # ...     log(f'service_foo')  async def call_bar():     # ...     log(f'service_bar')  async def feedback_handler_example():     set_log_extra({'handler': 'some-handler', 'user_id': 'some-guid'})     await asyncio.gather(         call_foo(),         call_bar(),     )     log(f'no explicit extra')  asyncio.run(feedback_handler_example())  # output: # text=&quot;service_foo&quot; # text=&quot;service_bar&quot; # text=&quot;no explicit extra&quot; handler=some-handler user_id=some-guid<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432\u044b\u0437\u043e\u0432 <code>asyncio.gather<\/code> \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u0442 <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-task.html#asyncio.gather\">\u043d\u043e\u0432\u0443\u044e \u0442\u0430\u0441\u043a\u0443<\/a>, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u043b <code>set_log_extra<\/code>, \u0438, \u043a\u0430\u043a \u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435, \u0443 \u043d\u0435\u0451 \u043d\u0435\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430, \u0445\u0440\u0430\u043d\u044f\u0449\u0435\u0433\u043e <code>extra<\/code>-\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, <code>asyncio<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u0430\u0441\u0442\u043e\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u0441\u043e\u043a \u0434\u043b\u044f event-loop\u2019\u0430 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>loop.set_task_factory<\/code>. \u0415\u0441\u043b\u0438 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0439 \u0442\u0430\u0441\u043a\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0442\u044c \u0435\u0439 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u043e\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438, \u043c\u044b \u0440\u0435\u0448\u0438\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u00ab\u0442\u0435\u0440\u044f\u044e\u0449\u0435\u0433\u043e\u0441\u044f\u00bb <code>extra<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\"># ...  def log_extra_factory():     default_task_factory = asyncio.Task      @functools.wraps(default_task_factory)     def custom_task_factory(loop, coro):         child_task = default_task_factory(coro, loop=loop)         if not loop.is_running():             return child_task         current_extra = get_log_extra()         set_log_extra(current_extra, task=child_task)         return child_task      return custom_task_factory  async def feedback_handler_example():     set_log_extra({'handler': 'some-handler', 'user_id': 'some-guid'})     await asyncio.gather(         call_foo(),         call_bar(),     )     log(f'no explicit extra')  async def main():     asyncio.get_event_loop().set_task_factory(log_extra_factory())     await feedback_handler_example()  asyncio.run(main())  # output: # text=&quot;fetch_data_from_db&quot; handler=some-handler user_id=some-guid # text=&quot;fetch_data_from_service&quot; handler=some-handler user_id=some-guid # text=&quot;no explicit extra&quot; handler=some-handler user_id=some-guid<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0446\u0435\u043b\u043e\u043c \u043c\u044b \u0443\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435. \u041e\u0434\u043d\u0430\u043a\u043e \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043c\u043e\u0434\u0443\u043b\u044e <a href=\"https:\/\/docs.python.org\/3\/library\/contextvars.html\">contextvars<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 Python 3.7, \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0442\u0430\u0441\u043e\u043a: \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0438 \u0431\u0435\u0437 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u043e\u043f\u0430\u0441\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c\u0438.<\/p>\n<p>  <\/p>\n<h2 id=\"contextvars\">Contextvars<\/h2>\n<p>  <\/p>\n<p>\u0412 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u0441\u043c\u044b\u0441\u043b\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u0445\u043e\u0436\u0438 \u043d\u0430 threadlocal-\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>threading.local<\/code>-\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043f\u043e\u0442\u043e\u043a\u0430, \u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>contextvars.ContextVar<\/code> \u2014 \u043e\u0442 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 (\u0438\u043d\u0441\u0442\u0430\u043d\u0441\u0430 <code>contextvars.Context<\/code>), \u043a\u043e\u0438\u0445 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">from contextvars import ContextVar, Context  ctx_var = ContextVar('ctx_var')  def get_name():     # \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 ctx_var \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0432\u0438\u0441\u0435\u0442\u044c \u043e\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430     return ctx_var.get()  for name in ['Alice', 'Bob']:     ctx = Context()     # ctx.run() \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 callable \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 ctx     # \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 ctx_var \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 ctx     ctx.run(ctx_var.set, name)     # \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c get_name \u0432\u043d\u0443\u0442\u0440\u0438 ctx     print(ctx.run(get_name))  # output: # Alice # Bob<\/code><\/pre>\n<p>  <\/p>\n<p>\u0423 <code>contextvars<\/code> \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0432\u0430\u0436\u043d\u044b\u0445 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u043d\u0430\u043c \u0440\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441 <code>log_extra<\/code>:<\/p>\n<p>  <\/p>\n<h3 id=\"vstroennaya-integraciya-s-asyncio\">\u0412\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 asyncio<\/h3>\n<p>  <\/p>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 Python 3.7 \u043c\u0435\u0442\u043e\u0434\u044b <code>Loop.call_{at,later,soon}<\/code> (\u0430 \u0442\u0430\u043a\u0436\u0435 <code>Future.add_done_callback<\/code>), \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u0437\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 event-loop\u2019\u0430, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0442 \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>context<\/code>, \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\"># asyncio pseudocode def call_soon(self, callback, *args, context=None):     if context is None:         # use copy of current context by default         context = contextvars.copy_context()     # ... later     context.run(callback, *args)<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0442\u0435\u043f\u0435\u0440\u044c \u043a\u0430\u0436\u0434\u0430\u044f \u0442\u0430\u0441\u043a\u0430 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0432\u0441\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0432 \u00ab\u0441\u0432\u043e\u0451\u043c\u00bb \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0440\u0430\u0432\u0435\u043d \u043a\u043e\u043f\u0438\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\"># asyncio pseudocode class Task:     def __init__(self, coro):         ...         # Get the current context snapshot.         self._context = contextvars.copy_context()         self._loop.call_soon(self._step, context=self._context)      def _step(self, exc=None):         ...         # Every advance of the wrapped coroutine is done in         # the task's context.         self._loop.call_soon(self._step, context=self._context)         ...<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u0412\u0441\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0430\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0435\u0434\u0438\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435.<\/li>\n<li>\u0414\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0442\u0430\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435-\u043a\u043e\u043f\u0438\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0442\u0430\u0441\u043a\u0438. \u0414\u0435\u0442\u0438 \u0438\u043c\u0435\u044e\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a\u043e \u0432\u0441\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f, \u043e\u0434\u043d\u0430\u043a\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u0431\u0435\u0437 \u0432\u043b\u0438\u044f\u043d\u0438\u044f \u043d\u0430 \u0441\u043e\u0441\u0435\u0434\u0435\u0439.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u043a\u0430\u043a \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">import asyncio, contextvars  ctx_var = contextvars.ContextVar('ctx_var')  async def main():     ctx_var.set('aaa')     print(f'ctx_var in main: {ctx_var.get()}')     await same_task()     print(f'ctx_var in main after same_task call: {ctx_var.get()}')     await asyncio.Task(other_task())     print(f'ctx_var in main after asyncio.Task(other_task()) call: {ctx_var.get()}')  async def same_task():     print(f'ctx_var in same_task: {ctx_var.get()}')     ctx_var.set('bbb')     print(f'ctx_var in same_task after set: {ctx_var.get()}')  async def other_task():     print(f'ctx_var in other_task before set: {ctx_var.get()}')     ctx_var.set('ccc')     print(f'ctx_var in other_task after set: {ctx_var.get()}')  asyncio.run(main())  # output: # ctx_var in main: aaa # ctx_var in same_task: aaa # ctx_var in same_task after set: bbb # ctx_var in main after same_task call: bbb  # (1) # ctx_var in other_task before set: bbb  # (2) # ctx_var in other_task after set: ccc # ctx_var in main after asyncio.Task(other_task()) call: bbb  # (3)<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0442\u0440\u0438 \u0432\u0430\u0436\u043d\u044b\u0445 \u0430\u0441\u043f\u0435\u043a\u0442\u0430:<\/p>\n<p>  <\/p>\n<ol>\n<li>\u041a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 <code>same_task<\/code>, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0430\u044f\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u0439 \u0442\u0430\u0441\u043a\u0435 \u0441 <code>main<\/code>, \u043f\u043e\u0432\u043b\u0438\u044f\u043b\u0430 \u043d\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>ctx_var<\/code> \u0434\u043b\u044f \u043d\u0438\u0445 \u043e\u0431\u0435\u0438\u0445, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435.<\/li>\n<li>\u041a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 <code>other_task<\/code>, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0430\u044f\u0441\u044f \u0432 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0439 \u0442\u0430\u0441\u043a\u0435 \u043e\u0442 <code>main<\/code>, \u0443\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043b\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>ctx_var<\/code>, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0435 \u0432 <code>main<\/code>.<\/li>\n<li>\u041e\u0434\u043d\u0430\u043a\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f <code>ctx_var<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 <code>other_task<\/code> \u043d\u0435 \u043f\u043e\u0432\u043b\u0438\u044f\u043b\u0430 \u043d\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 <code>main<\/code>, \u0442\u0430\u043a \u043a\u0430\u043a \u0434\u043e\u0447\u0435\u0440\u043d\u044f\u044f \u0442\u0430\u0441\u043a\u0430 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0438\u0445 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u043c\u0438.<\/li>\n<\/ol>\n<p>  <\/p>\n<h3 id=\"kopirovanie-konteksta-za-o1\">\u041a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0437\u0430 O(1)<\/h3>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043a\u0430\u0436\u0434\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0430\u0441\u043a\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u0432\u0430\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043b\u0435\u0433\u043a\u043e\u0432\u0435\u0441\u043d\u044b\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044c <code>contextvars<\/code> \u0445\u0440\u0430\u043d\u0438\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e immutable-\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Hash_array_mapped_trie\">HAMT<\/a> (hash array mapped trie), \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">import contextvars, time class Timer:     def __enter__(self):         self.start = time.perf_counter()     def __exit__(self, *args):         print(time.perf_counter() - self.start)  LOOP_COUNT = 1000 CTXVAR_COUNT = 42000  print(f'Context with {len(contextvars.copy_context())} variables') with Timer():     for _ in range(LOOP_COUNT):         contextvars.copy_context()  cvars = [contextvars.ContextVar(f'cvar_{i}', default=i) for i in range(CTXVAR_COUNT)] for v in cvars:     v.set('somevalue')  print(f'Context with {len(contextvars.copy_context())} variables') with Timer():     for _ in range(LOOP_COUNT):         contextvars.copy_context() # output # Context with 0 variables # 0.00015302500000000108 # Context with 42000 variables # 0.00015314099999999835<\/code><\/pre>\n<p>  <\/p>\n<h3 id=\"primenyaem-contextvars-k-extra\">\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c contextvars \u043a <code>extra<\/code><\/h3>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043d\u0430\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0434\u043b\u044f \u0442\u0430\u0441\u043e\u043a, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0439 \u0444\u0430\u043a\u0442\u043e\u0440\u0438 \u2014 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>get_log_extra<\/code> \u0438 <code>set_log_extra<\/code> \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u043e\u0431\u0451\u0440\u0442\u043a\u0430\u043c\u0438 \u043d\u0430\u0434 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">import asyncio import functools import contextvars  _log_extra_data = contextvars.ContextVar('_log_extra_data')  def set_log_extra(log_extra):     _log_extra_data.set(log_extra)  def get_log_extra():     return _log_extra_data.get()  def log(text):     extra_tskv = '\\t'.join(f'{k}={v}' for k, v in get_log_extra().items())     msg = f'text=&quot;{text}&quot;\\t{extra_tskv}'     print(msg)  async def call_foo():     log(f'service_foo')  async def call_bar():     log(f'service_bar')  async def feedback_handler_example():     set_log_extra({'handler': 'some-handler', 'user_id': 'some-guid'})     await asyncio.gather(         call_foo(),         call_bar(),     )     log(f'no explicit extra')  async def main():     await feedback_handler_example()  asyncio.run(main()) # output # text=&quot;service_foo&quot;      handler=some-handler    user_id=some-guid # text=&quot;service_bar&quot;      handler=some-handler    user_id=some-guid # text=&quot;no explicit extra&quot;        handler=some-handler    user_id=some-guid<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u0438\u043b\u044c\u043d\u043e \u043b\u0443\u0447\u0448\u0435. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0435\u0440\u0435\u0434 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0435\u043c \u0432 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 \u043f\u0430\u0440\u0430 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u0448\u0442\u0440\u0438\u0445\u043e\u0432.<\/p>\n<p>  <\/p>\n<h3 id=\"izmenit-extra-dlya-konkretnoy-taski\">\u0418\u0437\u043c\u0435\u043d\u0438\u0442\u044c <code>extra<\/code> \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0442\u0430\u0441\u043a\u0438<\/h3>\n<p>  <\/p>\n<p>\u0412\u0430\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u00ab\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u00bb \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043d\u0435 \u0432\u043b\u0435\u0447\u0451\u0442 \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 \u0433\u043b\u0443\u0431\u043e\u043a\u043e\u0433\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0432\u043b\u0438\u044f\u0442\u044c \u043d\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u043c\u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0443 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c response_id \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 extra-\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">async def external_call():     # ...     get_log_extra()['response_id'] = 'some_external_response_id'     log('log from external call')  async def feedback_handler_example():     set_log_extra({'handler': 'some-handler', 'user_id': 'some-guid'})     await asyncio.Task(external_call())     log('log from parent task ')  async def main():     await feedback_handler_example()  asyncio.run(main()) # output # text=&quot;log from external call&quot;  handler=some-handler    user_id=some-guid       response_id=some_external_response_id # text=&quot;log from parent task&quot;     handler=some-handler    user_id=some-guid       response_id=some_external_response_id<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u044f\u044f \u0442\u0430\u0441\u043a\u0430 \u043f\u043e\u0432\u043b\u0438\u044f\u043b\u0430 \u043d\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f. \u0417\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u0442\u0430\u043a\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043d\u0435\u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043b\u0443\u0447\u0448\u0435 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043a\u0441\u0442\u0440\u0443 \u0447\u0435\u0440\u0435\u0437 \u0433\u043b\u0443\u0431\u043e\u043a\u043e\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">import copy  # ...  def get_log_extra(should_copy=False):     extra = _log_extra_data.get()     if should_copy:         return copy.deepcopy(extra)     return extra  def update_log_extra(update):     current_extra = get_log_extra(should_copy=True)     set_log_extra({**current_extra, **update})  async def external_call():     # ...     update_log_extra({'response_id': 'some_external_response_id'})     log('log from external call')  async def feedback_handler_example():     set_log_extra({'handler': 'some-handler', 'user_id': 'some-guid'})     await asyncio.Task(external_call())     log(' log from parent task)  async def main():     await feedback_handler_example()  asyncio.run(main()) # output # text=&quot;log from external call&quot;  handler=some-handler    user_id=some-guid       response_id=some_external_response_id # text=&quot;log from parent task&quot;     handler=some-handler    user_id=some-guid<\/code><\/pre>\n<p>  <\/p>\n<h3 id=\"kontekstnye-peremennye-i-tredpul\">\u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u0442\u0440\u0435\u0434\u043f\u0443\u043b<\/h3>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043e\u0442\u0434\u0430\u0442\u044c \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0432 \u0442\u0440\u0435\u0434\u043f\u0443\u043b (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u0437-\u0437\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0430 \u0443 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438). \u0417\u0434\u0435\u0441\u044c \u043d\u0430\u0441 \u0436\u0434\u0451\u0442 \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u044b\u0439 \u0441\u044e\u0440\u043f\u0440\u0438\u0437: \u043f\u043e\u0442\u043e\u043a\u0438 \u0438\u0437 \u043f\u0443\u043b\u0430 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432 \u00ab\u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c\u00bb \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u0438 \u043d\u0435 \u0431\u044b\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u00ab\u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c\u00bb \u043f\u043e\u0442\u043e\u043a\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">import asyncio import functools import contextvars  _log_extra_data = contextvars.ContextVar('_log_extra_data')  # ...  def log(text):     extra_tskv = '\\t'.join(f'{k}={v}' for k, v in get_log_extra().items())     msg = f'text=&quot;{text}&quot;\\t{extra_tskv}'     print(msg)  async def feedback_handler_example():     set_log_extra({'handler': 'some-handler', 'user_id': 'some-guid'})     await asyncio.get_event_loop().run_in_executor(None, log, 'some-info')     log(f'no explicit extra')  async def main():     await feedback_handler_example()  asyncio.run(main()) # output: # Traceback (most recent call last): # ... #   File &quot;contextvars_threadpool.py&quot;, line 11, in get_log_extra #     return _log_extra_data.get() # LookupError: &lt;ContextVar name='_log_extra_data' at 0x1101a63b0&gt;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u043f\u043e\u0442\u043e\u043a\u0435 \u0438\u0437 \u043f\u0443\u043b\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>_log_extra_data<\/code> \u043d\u0438 \u0440\u0430\u0437\u0443 \u043d\u0435 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043b\u043e\u0441\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434 <code>get()<\/code> \u043f\u043e\u0434\u043d\u0438\u043c\u0430\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 <code>LookupError<\/code>. \u0414\u043b\u044f \u043d\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0442\u0430\u043a\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0441\u043e\u043c\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c: \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u043e\u043a\u0438 \u0432 \u043f\u0443\u043b\u0435 \u0432\u0435\u043b\u0438 \u0441\u0435\u0431\u044f \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0442\u0430\u0441\u043a\u0438, \u0442. \u0435. \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f. \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u0432\u043e\u0439 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043f\u0443\u043b, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0438\u0439 target-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432 \u043a\u043e\u043f\u0438\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u0438 \u0437\u0430\u0434\u0430\u0434\u0438\u043c \u0435\u0433\u043e \u043f\u0443\u043b\u043e\u043c \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0434\u043b\u044f event-loop\u2019\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">import asyncio import concurrent.futures import contextvars import functools import typing  class CtxCopyThreadPoolExecutor(concurrent.futures.ThreadPoolExecutor):     def submit(             self, fn: typing.Callable, *args, **kwargs,     ) -&gt; concurrent.futures.Future:         ctx = contextvars.copy_context()         return super().submit(             ctx.run,             functools.partial(fn, *args, **kwargs),         )  # ... async def feedback_handler_example():     set_log_extra({'handler': 'some-handler', 'user_id': 'some-guid'})     await asyncio.get_event_loop().run_in_executor(None, log, 'some-info')     log(f'no explicit extra')  async def main():     asyncio.get_event_loop().set_default_executor(CtxCopyThreadPoolExecutor())     await feedback_handler_example()  asyncio.run(main()) # text=&quot;from threadpool&quot;        handler=some-handler    user_id=some-guid # text=&quot;no explicit extra&quot;      handler=some-handler    user_id=some-guid<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"itogi\">\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>  <\/p>\n<p>\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0435 \u00ab\u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e\u00bb <code>extra<\/code> \u043f\u0440\u0438\u0432\u0435\u043b\u043e \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443: \u043c\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0442\u0435\u0440\u044f\u0435\u043c \u043b\u043e\u0433\u0438 \u043f\u0440\u0438 \u0440\u0430\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0438 \u0438\u043d\u0446\u0438\u0434\u0435\u043d\u0442\u043e\u0432 \u0432 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435, \u043d\u0435 \u0442\u0430\u0441\u043a\u0430\u0435\u043c \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0441\u043a\u0432\u043e\u0437\u044c \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u044b \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0438 \u043d\u0435 \u0442\u0440\u0430\u0442\u0438\u043c \u0441\u0438\u043b\u044b \u043d\u0430 \u043e\u0442\u043b\u043e\u0432 \u0435\u0433\u043e \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u043d\u0430 \u0440\u0435\u0432\u044c\u044e.<\/p>\n<p>  <\/p>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u043c\u043d\u0435\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430\u0441\u0447\u0451\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445: \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u043d\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043d\u0430 \u0441\u0442\u0435\u0440\u043e\u0438\u0434\u0430\u0445, \u0435\u0449\u0451 \u0438 \u0441 \u043c\u0430\u0433\u0438\u0435\u0439 \u0432 \u0432\u0438\u0434\u0435 \u043d\u0435\u044f\u0432\u043d\u043e\u0433\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438.<\/p>\n<p>  <\/p>\n<p>\u041c\u044b \u0441\u0447\u0438\u0442\u0430\u0435\u043c, \u0447\u0442\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043f\u043e\u043b\u044c\u0437\u0430 \u044d\u0442\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 \u043f\u0435\u0440\u0435\u0432\u0435\u0448\u0438\u0432\u0430\u0435\u0442 \u0438\u0434\u0435\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438, \u043e\u0434\u043d\u0430\u043a\u043e \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0434\u044b \u0443\u0441\u043b\u044b\u0448\u0430\u0442\u044c \u0432\u0430\u0448\u0438 \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/yandex\/blog\/526322\/\"> https:\/\/habr.com\/ru\/company\/yandex\/blog\/526322\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ve\/yy\/dv\/veyydvnoed0iszivpdazjypyets.png\"><\/p>\n<p>  <\/p>\n<p>\u041c\u044b \u0432 \u042f\u043d\u0434\u0435\u043a\u0441.\u0422\u0430\u043a\u0441\u0438 \u043b\u044e\u0431\u0438\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043b\u043e\u0433\u0438. \u0415\u0449\u0451 \u0431\u043e\u043b\u044c\u0448\u0435 \u043c\u044b \u043b\u044e\u0431\u0438\u043c, \u043a\u043e\u0433\u0434\u0430 \u043b\u043e\u0433\u0438 \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u043d\u0430\u043c \u0440\u0430\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435. \u041f\u0440\u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0432 \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0442\u044b\u0441\u044f\u0447 RPS \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u043b\u043e\u0433-\u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u043c\u0430\u043b\u043e. \u0425\u043e\u0447\u0435\u0442\u0441\u044f \u0443\u043c\u0435\u0442\u044c \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u043e\u0433\u0438 \u043f\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e, \u0432\u0438\u0434\u0435\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0433\u043e API, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u0433\u043b\u0443\u0431\u043b\u044f\u0442\u044c\u0441\u044f \u0432 \u043b\u043e\u0433\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0430\u043a\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043a\u0430\u0436\u0434\u0430\u044f \u043b\u043e\u0433-\u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435 \u0441\u043e\u043f\u0440\u043e\u0432\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439: id \u0437\u0430\u043a\u0430\u0437\u0430, \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0437\u0430\u0431\u044b\u0432\u0430\u044e\u0442 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043f\u0440\u0438 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-312573","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/312573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=312573"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/312573\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=312573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=312573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=312573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}