Page 1 of 1
WaitNetworkEvent (the PB Server example uses 100% CPU)
Posted: Wed Jun 08, 2016 10:39 am
by Keya
the Network Server example in the PB helpfile uses 100% CPU because it polls NetworkServerEvent(). When I add a Delay(1) to the end of the polling loop that brings CPU back down to 0%, so maybe the documentation should mention something about that?
anyway it made me wonder - and i dont know how easy or practical it is, but it might be good to have a WaitNetworkEvent, similar to how WaitWindowEvent behaves but for NetworkServerEvent/NetworkClientEvent? to complement the current polling loop option

Re: WaitNetworkEvent (the PB Server example uses 100% CPU)
Posted: Sat Jun 11, 2016 12:43 am
by Lunasole
The problem is that even if you add Delay() with only 1ms, it will make your network server very slow ^^
So better should be dynamically adjusting delay, depending of is network idle (can use delay to decrease CPU usage) or there is any data sending/receiving (delay removed)
Something WaitNetworkEvent() would be nice of course
Re: WaitNetworkEvent (the PB Server example uses 100% CPU)
Posted: Sat Jun 11, 2016 2:24 am
by Keya
The problem is that even if you add Delay() with only 1ms, it will make your network server very slow ^^
So better should be dynamically adjusting delay
but if I Delay(more than 1) - assuming thats what you meant by dynamic - it'll just makes my app slower to respond? might even cause a bottleneck with several connections at the same time?
Re: WaitNetworkEvent (the PB Server example uses 100% CPU)
Posted: Sat Jun 11, 2016 4:41 am
by nco2k
use a delay only when there is no event (#PB_NetworkEvent_None).
c ya,
nco2k
Re: WaitNetworkEvent (the PB Server example uses 100% CPU)
Posted: Thu Apr 27, 2023 4:43 pm
by Taz
nco2k wrote: Sat Jun 11, 2016 4:41 am
use a delay only when there is no event (#PB_NetworkEvent_None).
This also slows down the application:
Code: Select all
Event = NetworkServerEvent()
If Event
...
Else
Delay(1)
EndIf
Re: WaitNetworkEvent (the PB Server example uses 100% CPU)
Posted: Thu Apr 27, 2023 5:11 pm
by Quin
How are you noticing the delay? I'm spamming my server with data (even splitting it using StringField for packet buffering) and not noticing. Perhaps i'm testing wrong, though.
All that said though I agree, a WaitNetworkEvent() function would be extremely handy.
Re: WaitNetworkEvent (the PB Server example uses 100% CPU)
Posted: Thu Apr 27, 2023 9:05 pm
by infratec
WaitNetworkEvent() only optional.
Because how you will terminate such a process?
Normally I do the network stuff in a thread.
With WaitNetworkEvent() I have to kill the thread. Very ugly.
Re: WaitNetworkEvent (the PB Server example uses 100% CPU)
Posted: Thu Apr 27, 2023 9:36 pm
by Quin
Yeah, I'd want it to work like (Wait)WindowEvent(), so we have both a wait and non-wait version.
Re: WaitNetworkEvent (the PB Server example uses 100% CPU)
Posted: Fri Apr 28, 2023 12:41 am
by jassing
Quin wrote: Thu Apr 27, 2023 9:36 pm
Yeah, I'd want it to work like (Wait)WindowEvent(), so we have both a wait and non-wait version.
Couldn't you just code this? You really only want to delay when there is no event, otherwise, you want it to be as fast as possible, so I wouldn't.. I do the 2nd example.
Something like:
Code: Select all
Procedure WaitNetworkServerEvent(nTimeout, ServerID)
Protected nEvent, now=ElapsedMilliseconds()
Repeat
nEvent = NetworkServerEvent(ServerID)
If nEvent<>#PB_NetworkEvent_None
break
EndIf
delay(1)
Until ElapsedMilliseconds()-now >= nTimeout
ProcedureReturn nEvent
EndProcedure
Code: Select all
Repeat
nEvent = NetworkServerEvent()
Select nEvent
Case #PB_NetworkEvent_Data
Case #PB_NetworkEvent_Connect
Case #PB_NetworkEvent_Disconnect
Case #PB_NetworkEvent_None
Delay(1)
EndSelect
Until ExitSignal
that way 'data' is prioritized, with 'connect' the 2nd most important one...
Just my $.02
Re: WaitNetworkEvent (the PB Server example uses 100% CPU)
Posted: Fri Apr 28, 2023 3:34 am
by Taz
Sorry, I think my statement was wrong. The error is in my program, I will investigate it.