Anvil.server.context.client.type

What I’m trying to do:
Recognize when a function is done in a background

What I’ve tried and what’s not working:

Code Sample:

if anvil.server.context.client.type != "background_task":
   print(anvil.server.context.client.type)
  #<Do stuff>

the print statement is returning “browser” even though the log is in a background task. Possibly a bug?

Hmm…can you provide a clone link to reproduce this?

When I try

"browser" != "background_task"

I get

True

as expected. Am I missing something?

I am working on it.

I think the issue arises because I am attempting to generate a PDF in the background task and the check is in that PDF form, but I’ll get the clone shortly!

The issue is that the call anvil.server.context.client.type is in a background task, but is returning “browser”

Here it is!

the context.type call is in the PDFForm.

Aha, yes! PDFs are rendered in a browser context - even if the rendering is triggered from a background task!

What are you trying to do with this information? Perhaps there’s a better way to work out whether you’re rendering a PDF…

2 Likes

Through a series of mixed imports, I have a Global import that attempts to login when generating the PDF. This hangs up the process and causes a failure. I am trying to avoid that logic by checking if it is in a background task.

I could trace exactly where it is getting imported and restructure a bit, but I thought using the context would suffice.

I use an argument in the form __init__. Something like this:
image

Then, when the form is printed, I tell the form whether it’s being printed or rendered on the browser for a preview:
image

1 Like

Made an easy work around by making logging in a method of the global object versus a default call in the __init__

This was probably the better thing to do overall anyways!

Thank you for setting my assumptions straight!