LucaMs Expert Licensed User Longtime User Oct 10, 2015 #1 Someone knows that I would avoid bottlneck in my web server. So, I'm trying to run code taking advantage of some WebSockect threads; I mean that I use CallSubDelayed from inside a WebSocket class to call routines of a code module. There is a "little" problem: I can not call function methods, since CallSubDelayed does not returns values. For example, I have a code module with functions to get records from a db or simply to know if a user exists. I could use global variables (i.s. gUserExists As Boolean), but I don't know when the db query will be completed. [In programming we are obliged to do somersaults: sooner or later we will fall without a safety net... Madhouse, wait, I'm coming ]
Someone knows that I would avoid bottlneck in my web server. So, I'm trying to run code taking advantage of some WebSockect threads; I mean that I use CallSubDelayed from inside a WebSocket class to call routines of a code module. There is a "little" problem: I can not call function methods, since CallSubDelayed does not returns values. For example, I have a code module with functions to get records from a db or simply to know if a user exists. I could use global variables (i.s. gUserExists As Boolean), but I don't know when the db query will be completed. [In programming we are obliged to do somersaults: sooner or later we will fall without a safety net... Madhouse, wait, I'm coming ]
R Roycefer Well-Known Member Licensed User Longtime User Oct 10, 2015 #2 You can post the results to global variables (not such a great idea) or use callbacks (a better idea). Upvote 0
You can post the results to global variables (not such a great idea) or use callbacks (a better idea).
LucaMs Expert Licensed User Longtime User Oct 10, 2015 #3 I don't understand how to use callbacks. I have a Websocket class (object - which "has" its own thread). From there I call a function which is in a code module; I use CallSubDelayed to execute that function in the websocket thread: Code: Copy ' wsh - WebSocket class CallSubDelayed(modDB, "MyFunction") Code: Copy ' modDB - code module MyFunction(...) As Boolean As I wrote, wsh can not knows when MyFunction (which executes a db query) will be completed, then a gloabal variable can not be useful. Callbacks? [Note that, very probably, there will be many situations like that, in my web server] Upvote 0
I don't understand how to use callbacks. I have a Websocket class (object - which "has" its own thread). From there I call a function which is in a code module; I use CallSubDelayed to execute that function in the websocket thread: Code: Copy ' wsh - WebSocket class CallSubDelayed(modDB, "MyFunction") Code: Copy ' modDB - code module MyFunction(...) As Boolean As I wrote, wsh can not knows when MyFunction (which executes a db query) will be completed, then a gloabal variable can not be useful. Callbacks? [Note that, very probably, there will be many situations like that, in my web server]
LucaMs Expert Licensed User Longtime User Oct 10, 2015 #4 Something like this: Code: Copy Public Sub UserExists(Caller As Object, UserName As String) As Boolean Private Result As Booleant ' execute query ' ... CallSub2(Caller, "UserExists", Result) End Sub ? Upvote 0
Something like this: Code: Copy Public Sub UserExists(Caller As Object, UserName As String) As Boolean Private Result As Booleant ' execute query ' ... CallSub2(Caller, "UserExists", Result) End Sub ?
LucaMs Expert Licensed User Longtime User Oct 10, 2015 #5 Probably (I have not tried) the code in the previous post could work, but each time I will need something like: Code: Copy If UserExists(UserName) Then ' do something Else ' do other End If I have to "continue" the code containing the call in the "event" routine! This looks worse than the infamous "GoTo" Upvote 0
Probably (I have not tried) the code in the previous post could work, but each time I will need something like: Code: Copy If UserExists(UserName) Then ' do something Else ' do other End If I have to "continue" the code containing the call in the "event" routine! This looks worse than the infamous "GoTo"
R Roycefer Well-Known Member Licensed User Longtime User Oct 10, 2015 #6 You could use a generic callback that includes some information about the method that called the callback, passed as a parameter. That way you don't have to write a million different callback methods, all that logic will be in one (large) method. This should be easier to maintain. But yes, multi-threaded event-driven programming is a bit different than single-threaded linear programming. It'll take some getting used to but performance-wise, it will be better than blocking a thread while waiting for a return value. Upvote 0
You could use a generic callback that includes some information about the method that called the callback, passed as a parameter. That way you don't have to write a million different callback methods, all that logic will be in one (large) method. This should be easier to maintain. But yes, multi-threaded event-driven programming is a bit different than single-threaded linear programming. It'll take some getting used to but performance-wise, it will be better than blocking a thread while waiting for a return value.
LucaMs Expert Licensed User Longtime User Oct 10, 2015 #8 I should sleep, sometimes! I must use CallSub, in my case, not CallSubDelayed, to execute the routine in the caller thread, and, fortunately, CallSub returns values! S O R R Y Upvote 0
I should sleep, sometimes! I must use CallSub, in my case, not CallSubDelayed, to execute the routine in the caller thread, and, fortunately, CallSub returns values! S O R R Y
Cableguy Expert Licensed User Longtime User Oct 10, 2015 #9 Did you try... MyResult = callsubdelayed (modB, bla,bla) And in the called sub have the return result?? Upvote 0
Did you try... MyResult = callsubdelayed (modB, bla,bla) And in the called sub have the return result??
LucaMs Expert Licensed User Longtime User Oct 10, 2015 #10 Cableguy said: Did you try... MyResult = callsubdelayed (modB, bla,bla) And in the called sub have the return result?? Click to expand... I, because: CallSubDelayed(s) does not return a value. Fortunately I need to use CallSub, since I need the routine called is executed in the thread of the caller module. Upvote 0
Cableguy said: Did you try... MyResult = callsubdelayed (modB, bla,bla) And in the called sub have the return result?? Click to expand... I, because: CallSubDelayed(s) does not return a value. Fortunately I need to use CallSub, since I need the routine called is executed in the thread of the caller module.