question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to start background_tasks on event startup

See original GitHub issue
@app.on_event("startup")
async def startup_event(background_tasks: BackgroundTasks):
    send_notification(background_tasks)
2021-05-12 15:31:40,350 - MainThread - ERROR - Traceback (most recent call last):
  File "F:\douyin_spider_fast_api\venv\lib\site-packages\starlette\routing.py", line 540, in lifespan
    async for item in self.lifespan_context(app):
  File "F:\douyin_spider_fast_api\venv\lib\site-packages\starlette\routing.py", line 481, in default_lifespan
    await self.startup()
  File "F:\douyin_spider_fast_api\venv\lib\site-packages\starlette\routing.py", line 516, in startup
    await handler()
TypeError: startup_event() missing 1 required positional argument: 'background_tasks'

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:10

github_iconTop GitHub Comments

2reactions
tusharjagtap-bccommented, Jul 22, 2021

@codemation That is an issue in my opinion. on_event(‘startup’) does not mean that you will always want to call a method without arguments on startup, does it?

In any case, a workaround to be able to start a background task on startup is highly appreciated. I haven’t found a way to do so yet.

1reaction
codemationcommented, Jul 22, 2021

The most obvious way to achieve referencing your method that takes arguments, is with a new method with the arguments statically defined:

@app.on_event('startup')
async def startup_event_setup():
    startup_event(background_tasks)

@tusharjagtap-bc It might help if you share what argument that you are passing into your startup event.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Background Tasks - FastAPI
You can define background tasks to be run after returning a response. This is useful for operations that need to happen after a...
Read more >
Create and register an in-process background task
Create and register an in-process task that runs in the same process as your foreground app.
Read more >
Start an async background daemon in a Python FastAPI app
try @app.on_event("startup") async def startup_event() -> None: """tasks to do at server startup""" ...
Read more >
Using background tasks to update your app - Apple Developer
To schedule a task to run in the background, enable the background modes in Xcode, identify the specific tasks that you need, and...
Read more >
How to run background tasks in ASP.NET Core Application
Background tasks using IHostedService · Firstly, the StartAsync method is called by the framework before the Configure method of the Startup ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found