2020-07-08 20:11:16

Hello,
I was messing around with Lucia (via Python) and I came across the idea of Callback functions. now, I"ve seen these before, however the wording confused me a little.
It tells me to pass a function to the class, however...

I'm asuming this would be a string of the function name, because if I directly called the function, it would just contain the returned information. However, I'm confused at just passing it a string. How does it trace this to a relative file path.
For example: I have some random functoin:

def myCallback ():
    print ("Stuff happens... Yea!")

Then, I pass that to a callback variable inside the Lucia Module.

ui = lucia.ui.menu2 () #This doesn't actually work because it isn't enabled.
ui.callbackFunction = "myCallback"

My confusion on this matter is that the calling of the actual Callback occurs in the run loop inside Lucia. How, then, does it know which actual function I am referring to wen I pass it a string such as this.
Am I misunderstanding the process? Is there a better way to do Callbacks?
Thank you.

----------
“Yes, sir. I am attempting to fill a silent moment with non-relevant conversation.”
“You don’t tell me how to behave; you’re not my mother!”
“Could you please continue the petty bickering? I find it most intriguing.” – Data (Star Trek: The Next Generation)

2020-07-08 20:59:09

You don't pass it the string. You pass it the function itself.  If I have:

def f():
    pass

Then f() calls the function, but f refers to the function.  You can do things like f2 = f, for example.  Or write functions that return functions even.

Try defining a function in the repl, then just typing its name, and you can see what I mean.

My Blog
Twitter: @ajhicks1992

2020-07-08 21:06:36

So what do they do? I'm guessing each time through the loop, the callback runs, does its thing and when it returns, the loop continues. Would it then be bad practice for your callbacks to have loops in them? I would think so, because it would halt the entire program until it was finished unless it was multi-threaded.

Facts with Tom MacDonald, Adam Calhoun, and Dax
End racism
End division
Become united

2020-07-08 21:12:21

@3
I don't entirely understand your question.  In general callbacks should try to exit quickly, though, yes.  But they're not otherwise different from the rest of your program in that regard, and in some non-game contexts, they can and will run for minutes to hours when called (i.e. Celery, tons of stuff people do with the multiprocessing module, every mapreduce framework in the history of parallel computing, etc).

The function is unevaluated  if you just name it without the parens, until the framework calls it.

My Blog
Twitter: @ajhicks1992

2020-07-08 21:44:17

Thank you. I was unaware you could refer to functions like that.

----------
“Yes, sir. I am attempting to fill a silent moment with non-relevant conversation.”
“You don’t tell me how to behave; you’re not my mother!”
“Could you please continue the petty bickering? I find it most intriguing.” – Data (Star Trek: The Next Generation)