Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors from background tasks are not printed #78

Closed
bartlomieju opened this issue Oct 17, 2018 · 5 comments
Closed

errors from background tasks are not printed #78

bartlomieju opened this issue Oct 17, 2018 · 5 comments
Labels
enhancement New feature or request hacktoberfest

Comments

@bartlomieju
Copy link

Given:

import responder
# import time <- notice missing import

api = responder.API()

@api.route("/")
def hello(req, resp):

    @api.background.task
    def sleep(s=1):
        time.sleep(s)
        print("slept!")

    sleep()
    resp.content = "processing"

if __name__ == '__main__':
    api.run()

When running sleep NameError is thrown but no output is shown in console.

@bartlomieju bartlomieju changed the title BUG: errors from console are not printed errors from background tasks are not printed Oct 17, 2018
@kennethreitz
Copy link
Owner

we need to add logging.

@kennethreitz kennethreitz added enhancement New feature or request hacktoberfest labels Oct 17, 2018
@condemil
Copy link
Contributor

The exception is happening in separate thread that is the reason it is not printed.

@kennethreitz What do you think will be the best approach to handle exception in futures? It is possible to have a custom ThreadPoolExecutor as it is proposed here.

@vuonghv
Copy link
Contributor

vuonghv commented Oct 23, 2018

@kennethreitz What about passing callback functions to @api.background.task to handle success and error case. I'm implementing this feature, and the following is demo api:

def on_success(result):
     print(result)

def on_error(error):
    print(error)

@api.background.task(on_success, on_error)
def sleep(s=10):
     # Do background tasks here

@pbsds
Copy link
Contributor

pbsds commented Oct 24, 2018

@kennethreitz What about passing callback functions to @api.background.task to handle success and error case. I'm implementing this feature, and the following is demo api:

def on_success(result):
     print(result)

def on_error(error):
    print(error)

@api.background.task(on_success, on_error)
def sleep(s=10):
     # Do background tasks here

Although neat, this is dangerously close the Deferred structures used in Twisted. We use async and await to avoid that callback mess. I say ensuring exception are printed should be enough. Leaving the error handling up to the user is a lot more readable. Creating a separate decorator for this kind of error handling should be trivial should the user want it.

@kennethreitz
Copy link
Owner

I feel if you want something more sussinct, you should just use async/await

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hacktoberfest
Projects
None yet
Development

No branches or pull requests

5 participants