Sign in to follow this  
NeverGiveup

Long execution warning

Recommended Posts

Hey i made a command to start resources but, i got warning on ythe console saying Warning: Long execution (cmstart), is this okay to ignore?

  
addCommandHandler("startup", function(p, cmd, suf) 
if suf == "job" then 
    if scripter[getAccountName(getPlayerAccount(p))] then 
       for i, res in ipairs(getResources()) do 
           local resName = getResourceName(res) 
           if resName:find("Job_") then 
              startResource(res, true) 
           end 
       end 
    end 
end 
end) 
  

Ps: i got 23 Job resources any method to get rid the warning?

Share this post


Link to post

Do you want start all the resource?

Share this post


Link to post

Nope only resources with name Job_...

There are 23 of them. When they all start, console says long execution. I just wanna know other method.

Share this post


Link to post

Try that.

local sRes = "job" 
local scripter = { 
    ["test"] = true, 
} 
addCommandHandler("startup", 
    function ( p ) 
        if scripter[getAccountName(getPlayerAccount(p))] then 
            for i, res in ipairs(getResources()) do 
                local resName = getResourceName(res) 
                if string.find(resName,sRes) then 
                    if getResourceState (getResourceFromName(sRes)) == running then return end 
                    startResource ( getResourceFromName(sRes) ) 
                end 
            end 
        end 
    end ) 

Share this post


Link to post

Your code is exactly same as mine, but urs nakes no sense.

How is it you put sRes variable instead of resName at line #11 and #12?

Cuz its always gonna be "job",

And the code wont start other loaded resources cuz if the loop sense the resource running then you return it. yiu just make it worse.

Share this post


Link to post

This should work:

addCommandHandler("startup", function(p, cmd, suf) 
if suf == "job" then 
    if scripter[getAccountName(getPlayerAccount(p))] then 
       for i, res in ipairs(getResources()) do 
           local resName = getResourceName(res) 
           if resName:find("Job_") then 
              local finalRes = getResourceFromName (res) 
              startResource(finalRes, true) 
           end 
       end 
    end 
end 
end) 

Share this post


Link to post

Call me crazy, but the OP is not asking for just working code. He's asking how to improve or whether it is good the code he already has, in order to fix or ignore that warning.

So I will try to answer your question: that kind of warning should never be ignored. It means that whatever your code does takes a long time to finish, and in slower servers it could also mean that your script gets aborted in the middle of its execution, throwing errors and not doing what you want it to do. You need to fix up your code, so it doesn't suck up all the limited CPU time the computer has.

I hear you asking: "but if my code does really need that time, how can I avoid this?" In that case you can't make your code take less time, but you can distribute the total time it takes in slices by using coroutines. They allow to pause and resume execution of functions, so while they are paused the server can use that CPU time to take care of another things, and in turn don't freeze everything else. When using coroutines, you are basically letting the PC to take a break from time to time, to allow him to dedicate his resources to another pieces of code.

So you should fix your code by making appropiate use of coroutines, in order to don't freeze your server and don't see warnings while it executes.

But if for some reason you are lazy, you can't really get to coroutines usage or you simply want to do performance tests, you can disable the MTA: SA hook that throws that warnings:

debug.sethook() 

But beware: disabling that hook is NOT a programming practice that should be used in production scripts. It is only intended for development purposes, not as a everyday solution to avoid coroutines. And it won't stop your server from freezing or even crashing; instead, it will make the problem worse, because if your script executes infinitely for some reason you will have to restart the entire server to stop it.

Share this post


Link to post

There is also possibility that all your "job_" scripts are doing something heavy on startup.

Try to check that.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.