1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

AsyncTask from RepeatingTask?

Discussion in 'Development' started by Levi, Oct 27, 2017.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    can I use asyncTask instead of repeating? because I have a task that use repeating(run a command every 2 minutes) and i'm getting too much lag. This is my result of timings :

    Code:
           0.03%     276.86%      0.14 s       138.43 ms     0.0           0.0k        Task: cd\Tasks\ResetAll(interval 24000)
           0.02%     116.81%      0.12 s        58.40 ms     0.0           0.0k        Task: cd\Tasks\ClearLagg(interval 2400)
    Main onEnable:

    PHP:
            $this->getServer()->getScheduler()->scheduleRepeatingTask(new ResetAll($this), 1200 20);
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new ClearLagg($this), 120 20);
    ClearLagg.php

    PHP:
    <?php


    namespace cd\Tasks;


    use 
    pocketmine\Server;

    use 
    pocketmine\scheduler\PluginTask;

    use 
    pocketmine\utils\TextFormat as TF;

    use 
    pocketmine\command\CommandExecuter;

    use 
    pocketmine\command\ConsoleCommandSender;

    use 
    cd\Main;


    class 
    ClearLagg extends PluginTask {

      

      

        public function 
    __construct(Main $plugin){

            
    parent::__construct($plugin);

            
    $this->plugin $plugin;

        }

        public function 
    onRun($tick){

      

            
    $this->plugin->getServer()->dispatchCommand(new ConsoleCommandSender"clearlagg clear");

            }

    }
    resetall's basically the same
     
    Last edited: Oct 27, 2017
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    no you can't
    simply put. (I am too tired to explain why, and you wouldn't understand it anyways) Just remember: ASYNC TASKS DO NOT JUST MAGICALLY GIVE INFINITE PERFORMANCE
    But instead of "Clearing Lagg" (I call it creating Lagg) You can simply prevent any entities from getting spawned with the help of events.

    and what does ResetAll do?
     
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    reset all mines of MineReset by @falk
     
  4. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    clearlagg
    clearlagg clears all entities/items on the ground
     
  5. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    yea, that's just going to give you a small lag spike every 20mins. 130ms is no problem every 20min.
    For your ClearLagg task, do what I said.
     
  6. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
  7. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
  8. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    If the task randomly exactly executes during the time they throw items on the ground they will be gone too.
     
  9. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Also, according to the timings you posted it is barely affecting your overall server performance.
     
  10. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    really? but the pct tick i got is red and over 100%
     
  11. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Yea, but that only happens every 2min/20min. It’s just a small lag spike.
     
    Levi likes this.
  12. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
  13. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Just make it so only X number of non-player-entities can spawn per minute.
     
  14. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    To prevent lag, you have to truly investigate where the source of lag comes from, not just blindly throw everything to AsyncTask. For example, to set a large area of blocks, you have to notice that the main lag comes from encoding a lot of block updates, and investigate how to prevent this lag (compress the block update packets in an AsyncTask and batch them, or just send the whole chunk compressed). Some things like sending packets inevitably have to be done on the main thread, so you have to figure out how to do the other things asynchronously, which requires quite a lot of hacking (and instability).
    You also have to pay attention to the cost of transferring data between threads. It can be quite costly, because all data have to be replicated to be sent from a thread to another.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.