It seems background tasks can't see globals?

Just to confirm, currently, background tasks always run in a separate Python interpreter, so they’ll never share global variables with your server calls.

The reason is to do with timeouts: If a server call takes too long, we need to kill it rather than letting it spin forever. Background tasks, however, can run for a long time. If we ran them in the same Python interpreter as your standard server functions, we would have to kill your background tasks whenever a server call timed out. (Likewise, if you called kill() on a background task, it could take out several other server calls as collateral damage! We try to avoid giving you unpleasant surprises like that :wink: )

To avoid these issues, we run each background task in a separate Python process. But the downside is that background tasks don’t share global variables with your server calls.

4 Likes