Parallel Python: What is a callback?

后端 未结 5 1547
别跟我提以往
别跟我提以往 2020-12-04 07:27

In Parallel Python it has something in the submit function called a callback (documentation) however it doesn\'t seem to explain it too well. I\'ve posted on

5条回答
  •  醉话见心
    2020-12-04 08:16

    The relevant spot in the docs:

    callback - callback function which will be called with argument 
            list equal to callbackargs+(result,) 
            as soon as calculation is done
    callbackargs - additional arguments for callback function
    

    So, if you want some code to be executed as soon as the result is ready, you put that code into a function and pass that function as the callback argument. If you don't need other arguments, it will be just, e.g.:

    def itsdone(result):
      print "Done! result=%r" % (result,)
    ...
    submit(..., callback=itsdone)
    

    For more on the callback pattern in Python, see e.g. my presentation here.

提交回复
热议问题