Skip to content

Commit

Permalink
Run consumers asynchronously (#89)
Browse files Browse the repository at this point in the history
* Run consumers asynchronously

* correct background tasks annotation
  • Loading branch information
pal03377 authored Aug 27, 2023
1 parent 2b120d0 commit ae9d1d8
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions athena/athena/endpoints.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# type: ignore # too much weird behavior of mypy with decorators
import inspect
from fastapi import Depends
from fastapi import Depends, BackgroundTasks
from pydantic import BaseModel, ValidationError
from typing import TypeVar, Callable, List, Union, Any, Coroutine, Type

Expand Down Expand Up @@ -71,6 +71,7 @@ def submissions_consumer(func: Union[
@authenticated
@with_meta
async def wrapper(
background_tasks: BackgroundTasks,
exercise: exercise_type,
submissions: List[submission_type],
module_config: module_config_type = Depends(get_dynamic_module_config_factory(module_config_type))):
Expand Down Expand Up @@ -101,11 +102,8 @@ async def wrapper(
if "module_config" in inspect.signature(func).parameters:
kwargs["module_config"] = module_config

# Call the actual consumer
if inspect.iscoroutinefunction(func):
await func(exercise, submissions, **kwargs)
else:
func(exercise, submissions, **kwargs)
# Call the actual consumer asynchronously
background_tasks.add_task(func, exercise, submissions, **kwargs)

return None
return wrapper
Expand Down Expand Up @@ -241,6 +239,7 @@ def feedback_consumer(func: Union[
@authenticated
@with_meta
async def wrapper(
background_tasks: BackgroundTasks,
exercise: exercise_type,
submission: submission_type,
feedbacks: List[feedback_type],
Expand All @@ -260,11 +259,8 @@ async def wrapper(
if "module_config" in inspect.signature(func).parameters:
kwargs["module_config"] = module_config

# Call the actual consumer
if inspect.iscoroutinefunction(func):
await func(exercise, submission, feedbacks, **kwargs)
else:
func(exercise, submission, feedbacks, **kwargs)
# Call the actual consumer asynchronously
background_tasks.add_task(func, exercise, submission, feedbacks, **kwargs)

return None
return wrapper
Expand Down

0 comments on commit ae9d1d8

Please sign in to comment.