WaitNetworkEvent (the PB Server example uses 100% CPU)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

WaitNetworkEvent (the PB Server example uses 100% CPU)

Post 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 :)
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: WaitNetworkEvent (the PB Server example uses 100% CPU)

Post 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
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: WaitNetworkEvent (the PB Server example uses 100% CPU)

Post 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?
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: WaitNetworkEvent (the PB Server example uses 100% CPU)

Post by nco2k »

use a delay only when there is no event (#PB_NetworkEvent_None).

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Taz
User
User
Posts: 75
Joined: Sat Jan 20, 2018 5:28 pm
Location: Germany

Re: WaitNetworkEvent (the PB Server example uses 100% CPU)

Post 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: :cry:

Code: Select all

Event = NetworkServerEvent()
If Event
	...
Else
	Delay(1)
EndIf
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: WaitNetworkEvent (the PB Server example uses 100% CPU)

Post 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.
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: WaitNetworkEvent (the PB Server example uses 100% CPU)

Post 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.
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: WaitNetworkEvent (the PB Server example uses 100% CPU)

Post by Quin »

Yeah, I'd want it to work like (Wait)WindowEvent(), so we have both a wait and non-wait version.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: WaitNetworkEvent (the PB Server example uses 100% CPU)

Post 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
Taz
User
User
Posts: 75
Joined: Sat Jan 20, 2018 5:28 pm
Location: Germany

Re: WaitNetworkEvent (the PB Server example uses 100% CPU)

Post by Taz »

Sorry, I think my statement was wrong. The error is in my program, I will investigate it.
Post Reply