Please help me reduce cpu usage on this litle app

8 posts in this topic

Rotahn 0

I've basically ripped this script from a post in the forum somewhere (sorry cant remember where from .. could of even been from the help file)

I have added/changed about 3 - 4 lines to get it to do what i require though

however..when i run it i notice that it uses loads of cpu time while waiting for a connection. There must be a better way for me to do this but as i'm a nooby i am looking for some someone to guide me..

CODE

TCPStartup()

;Starts the TCP connection. Basically allows other TCP functions to work

$Ip = @IPAddress1

$Port = 5900

;This waits for a connection on @IPAddress1 and port 5900, the default port

$Incoming = TCPListen($Ip, $Port)

;This "Listens" for a message from the client

While 1

;Starts the "While" loop

Do

;Declares a "Do" loop, which means it does an action until a specific condition is met

$Con = TCPAccept($Incoming)

;This "Accecpts" any incoming connections from other clients

Until $con > 0

;Does this until the variable $con has a value higher than zero

$whatever = 1

;Sets a dummy variable called $whatever. This will indicate when to exit the server

Do

;Starts another Do loop

$msg = TCPRecv($con, 5000)

;This waits for a value from the variable $con (The connected client). The value must be less than 5000 bytes

If $msg <> "" Then

;If there is ANY message then split the test into array and check what the request is and then perform the required function

$request = stringsplit($msg, "^")

sleep(500)

if $request[0] = 7 then controler($msg)

ElseIf $msg = "exit" Then

;If the client sends just the word "Exit" as the command then

$whatever = 2

;It changes the dummy variable $whatever to 2, so...

EndIf

Until $whatever <> 1

;If the Dummy doesn't equal 1 then

TCPShutdown()

;It stops the connection

Exit

;And exits

WEnd

;Stops the while loop and exits

;

;

;

;

; functions

;

;

;

func controler($msg)

$cleaned = StringReplace($msg, " ", "~")

MsgBox(0, "tcp", $cleaned,30)

$runthis = "c:\gwservers\controler.exe " & $cleaned

run($runthis)

EndFunc

thanks in advance guys..

btw there are game servers running off the same box so every ounce of cpu time is crucial

Share this post

Link to post

Share on other sites

weaponx 10

There was a post recently from a developer explaining how GuiGetMsg() really works. I can't seem to find it.

Anyways, it said something about GuiGetMsg() will run a tight loop until another application requests CPU time. If you start another cpu intensive program you will see the cpu usage of your script drop.

Share this post

Link to post

Share on other sites

Rotahn 0

There was a post recently from a developer explaining how GuiGetMsg() really works. I can't seem to find it.

Anyways, it said something about GuiGetMsg() will run a tight loop until another application requests CPU time. If you start another cpu intensive program you will see the cpu usage of your script drop.

You could also put a Sleep(100) inside your first Do...Until loop.

Thanks..

Will give that a try

Share this post

Link to post

Share on other sites

Rotahn 0

when i run the app the cpu time is negligable.. showing 0 most of the time which is what i would expect.. if left running listening for tcp connections after a few hours it starts using 20% cpu on a q6600 2.4!! i have tried the suggestions above but they dont seem to work. Also after killing the process either by exiting the script properly or just closing the process..sometimes.. leaves the the cores on the load of 20% still.

Now i did think that perhaps that killing the process and it still using the cpu @ 20% could be another rogue process..so.. i just booted up the machine with nothing at all loaded appart from 2003 server..ran the script and left it.. it did start using 20% after about 4 hours..i then closed the process to which it cleared all cpu usage..

so .. i loaded a game server up and ran it along side it.. after a few hours the script started to use the same amount of cpu.. i killed it and it still carried on using the same amount , but just on 2 cores. Its the game server i hear you say.. well.. this may well be but i have trioed this with 3 different game engines and all does the same..but not all the time.. its a srange one..