muffin 0.93.4__py3-none-any.whl → 0.94.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
muffin/app.py CHANGED
@@ -2,12 +2,13 @@
2
2
  from __future__ import annotations
3
3
 
4
4
  import logging
5
+ from contextvars import ContextVar
5
6
  from inspect import isawaitable, stack
6
7
  from logging.config import dictConfig
7
- from typing import TYPE_CHECKING, Any, Dict, Mapping, Union
8
+ from typing import TYPE_CHECKING, Any, Dict, Final, Mapping, Union
8
9
 
9
10
  from asgi_tools import App as BaseApp
10
- from asgi_tools.middleware import BACKGROUND_TASK
11
+ from asgi_tools._compat import aio_wait
11
12
  from modconfig import Config
12
13
 
13
14
  from muffin.constants import CONFIG_ENV_VARIABLE
@@ -21,6 +22,11 @@ if TYPE_CHECKING:
21
22
 
22
23
  from muffin.plugins import BasePlugin
23
24
 
25
+ BACKGROUND_TASK: Final["ContextVar[set[Awaitable] | None]"] = ContextVar(
26
+ "background_tasks",
27
+ default=None,
28
+ )
29
+
24
30
 
25
31
  class Application(BaseApp):
26
32
  """The Muffin Application."""
@@ -103,9 +109,9 @@ class Application(BaseApp):
103
109
  ):
104
110
  """Support background tasks."""
105
111
  await self.lifespan(scope, receive, send)
106
- bgtask = BACKGROUND_TASK.get()
107
- if bgtask is not None:
108
- await bgtask
112
+ bgtasks = BACKGROUND_TASK.get()
113
+ if bgtasks is not None:
114
+ await aio_wait(*bgtasks)
109
115
  BACKGROUND_TASK.set(None)
110
116
 
111
117
  def import_submodules(self, *submodules: str):
@@ -125,8 +131,8 @@ class Application(BaseApp):
125
131
  package_name = parent_frame.f_locals["__name__"]
126
132
  return import_submodules(package_name, *submodules)
127
133
 
128
- def run_background(self, task: Awaitable):
129
- """Await the given awaitable after the request is completed.
134
+ def run_after_response(self, task: Awaitable):
135
+ """Await the given awaitable after the response is completed.
130
136
 
131
137
  .. code-block:: python
132
138
 
@@ -143,7 +149,7 @@ class Application(BaseApp):
143
149
  async def send(request):
144
150
 
145
151
  # Schedule any awaitable for later execution
146
- app.run_background(send_email('user@email.com', 'Hello from Muffin!'))
152
+ app.run_after_response(send_email('user@email.com', 'Hello from Muffin!'))
147
153
 
148
154
  # Return response to a client immediately
149
155
  # The task will be executed after the response is sent
@@ -153,4 +159,7 @@ class Application(BaseApp):
153
159
  if not isawaitable(task):
154
160
  raise TypeError("Task must be awaitable") # noqa: TRY003
155
161
 
156
- BACKGROUND_TASK.set(task)
162
+ scheduled = BACKGROUND_TASK.get() or set()
163
+ scheduled.add(task)
164
+
165
+ BACKGROUND_TASK.set(scheduled)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: muffin
3
- Version: 0.93.4
3
+ Version: 0.94.2
4
4
  Summary: Muffin is a fast, simple and asyncronous web-framework for Python 3 (asyncio, trio, curio)
5
5
  Author-email: Kirill Klenov <horneds@gmail.com>
6
6
  License: MIT License
@@ -1,5 +1,5 @@
1
1
  muffin/__init__.py,sha256=F8Qp4ETX5FqI1832dj2moRAQ5hEb0TO9u4aRjSKXNjI,982
2
- muffin/app.py,sha256=-Gw9G_xL4WBhiB05aIvVPZSRNlXmORN0xX9o8C9CJPE,4774
2
+ muffin/app.py,sha256=tfmm9Dc-ZbUM9UwFUAYDIGPMcNJJU3hz5Wt8uwIylYM,5033
3
3
  muffin/constants.py,sha256=Q9Nm3Q4P3llL_irsvNQAkkD3CqlD4zKkGAM4nabzwrg,38
4
4
  muffin/errors.py,sha256=I-vKbMMBiMU07zPdKvoJKqA7s4xYAUA-4oZXrRMRzcM,701
5
5
  muffin/handler.py,sha256=c0b4pwsIF8jmqYwsWn3iuXR5BMwq2e6ASatGcPu9-N4,3667
@@ -9,9 +9,9 @@ muffin/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
9
  muffin/pytest.py,sha256=vnSEsQP8dJCFeS8nZklzAh7Stc3H-ng3jm6KlnAOMN0,2169
10
10
  muffin/types.py,sha256=wsUj5oAfqSZMoEf-wyFJLBlWa8Mc-eJGqKLr02HxuXE,153
11
11
  muffin/utils.py,sha256=4tdGNAmdEy6LPaf-5rLYx2YMZ5EOe6tXSqAC1GtG_TY,2422
12
- muffin-0.93.4.dist-info/LICENSE,sha256=xHPkOZhjyKBMOwXpWn9IB_BVLjrrMxv2M9slKkHj2hM,1082
13
- muffin-0.93.4.dist-info/METADATA,sha256=O3gdr3jLwZyfZT0w9Tp9fhnVP6ubXCvHUvmwipYmldg,12021
14
- muffin-0.93.4.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
15
- muffin-0.93.4.dist-info/entry_points.txt,sha256=Q3DAZGQk-y5n7HmzJ4FrkNizqTRE2OVXsniPQXzS0Bg,87
16
- muffin-0.93.4.dist-info/top_level.txt,sha256=b_xNEFyizTmSqZWCTHwcvbycGyADewdsnaLXo2Pq1Ck,7
17
- muffin-0.93.4.dist-info/RECORD,,
12
+ muffin-0.94.2.dist-info/LICENSE,sha256=xHPkOZhjyKBMOwXpWn9IB_BVLjrrMxv2M9slKkHj2hM,1082
13
+ muffin-0.94.2.dist-info/METADATA,sha256=205DzwT2ZUtnfwD7ki1HeFSkBQ5M3leSOX5cUU_RuSA,12021
14
+ muffin-0.94.2.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
15
+ muffin-0.94.2.dist-info/entry_points.txt,sha256=Q3DAZGQk-y5n7HmzJ4FrkNizqTRE2OVXsniPQXzS0Bg,87
16
+ muffin-0.94.2.dist-info/top_level.txt,sha256=b_xNEFyizTmSqZWCTHwcvbycGyADewdsnaLXo2Pq1Ck,7
17
+ muffin-0.94.2.dist-info/RECORD,,