Other WebApp - callings run in [...] thread

LucaMs

Expert
Licensed User
Longtime User
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:D:(... Madhouse, wait, I'm coming :confused:]
 

Roycefer

Well-Known Member
Licensed User
Longtime User
You can post the results to global variables (not such a great idea) or use callbacks (a better idea).
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
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:
B4X:
' wsh - WebSocket class
CallSubDelayed(modDB, "MyFunction")

B4X:
' 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

LucaMs

Expert
Licensed User
Longtime User
Probably (I have not tried) the code in the previous post could work, but each time I will need something like:

B4X:
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

Roycefer

Well-Known Member
Licensed User
Longtime User
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

Cableguy

Expert
Licensed User
Longtime User
Did you try...

MyResult = callsubdelayed (modB, bla,bla)

And in the called sub have the return result??
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Did you try...

MyResult = callsubdelayed (modB, bla,bla)

And in the called sub have the return result??


I, because:
upload_2015-10-10_12-46-46.png


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
Top