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

Solved AsyncTask

Discussion in 'Development' started by Miste, May 12, 2017.

  1. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    Is it bad to use async task every second ?
     
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    too generic question ftw.
    What do you mean by "use"?
    Do you schedule it every second? => Not that bad, but consider a thread
    Do you sleep inside the Task for 1 second and you have an infinite loop => BAD, using a Thread is mandatory
    And lastly, do you need to get results from the AsyncTask or do you just put some data into it and it does some stuff?
     
  3. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    I am just doing some MySQL stuff, and i schedule it by using a task i just want to know if it's bad for my VPS (global perf)
     
  4. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
  5. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Whatever you're querying to your DB cache it and do multiple querys in the task, don't have an async task running every sec. Cache the querys and have a task ex) every 5 sec with all the querys.
     
    Muqsit likes this.
  6. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    I know, but it's not every second but i schedule a task when a player join, quit and every 5 ecs
     
  7. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    What data are you writing to the DB?
     
  8. KnownUnown

    KnownUnown Spider Jockey Poggit Reviewer

    Messages:
    47
    GitHub:
    knownunown
    If you're doing SQL ops that frequently, it may be better to use a thread. This allows for better state management IMO, and would require less repeated task scheduling. The only downside is that you lose some nice constructs provided by PMMP in AsyncTasks. Of course, being in a different thread doesn't mean you can slack off caching or batching either, you still would need to handle that. The only "hard" part would be inter-thread communication.
     
  9. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    For what task ? Join/QUit or the task executed every 5 secs ?
     
  10. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Use PThreads and create a new thread don't use PocketMine to do it.
     
  11. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    There isn't really anything wrong with using async tasks frequently, as long as it is needed. If you create another thread to do it, you're just reinventing the async pool system.
    As long as you aren't spamming the worker threads too excessively, it's OK.
    If you need to do something self-repeating (i.e. schedule a new AsyncTask every time the last AsyncTask completes), it's totally acceptable that you use AsyncTask instead of starting your own thread, because basically, it's the same as occupying one of the worker threads; and if the worker threads are unable to maintain your high frequency of starting AsyncTasks, it will simply slow down re-scheduling because it won't start a new one until the last one is completed.
    If you want to start async tasks every join/quit, it is also OK. If your server is really so busy that you have one join per second, you should probably get a better machine and database that allows you to run more async workers and complete the database queries more quickly. This is then a hardware problem, because even if you make it a thread it's not gonna help.
     
  12. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Again: why? Mind explaining?
     
  13. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    Ok thanls !
     
  14. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Sure.
    Scheduling tasks from the main thread every second even if they are async can become laggy. If you have 1 async worker and schedule 40 async tasks? I did the same thing Miste is trying to do (writing data to MySQL remote DB every second) and my test server froze within a few min, I then switched to pthreads and used session vars to create a queue for my query. It worked magnificently and the lag was greatly reduced, PMMP is by far the strongest of the PHP server software's but isn't 100% stable (neither are any of the other software's); In my opinion PMMP isnt the greatest at managing memory and multiple threads and/or tasks. I'm not going to provide any code but I will provide some links, multi threading is an essential in some PHP applications and if you peruse PHP programming you most likely won't get very far with PocketMine plugins.

    Links:
    http://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications
    http://php.net/manual/en/book.pthreads.php
     
  15. KnownUnown

    KnownUnown Spider Jockey Poggit Reviewer

    Messages:
    47
    GitHub:
    knownunown
    My primary motivation for recommending a separate thread was on the basis that you would overschedule a worker thread in the case of very frequent (<=1s between) joins. Of course, you can also start your own worker (or maybe a few) and schedule to them. Personal preference, really.
     
    Jack Noordhuis likes this.
  16. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Well, maybe this is not what async-workers should have meant, but this will be fixed if you increase the number of async workers used on the server.
    Indeed, this seems to be deceiving the purpose of "async-workers", which is to approximately use one CPU per worker. It's possible to dedicate some workers for your blocking operations (we're discussing that internally), but if it's just for private servers, manually increasing the number of async workers is good enough.
     
  17. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    How to increase the number ? And what it will do if i increase it ? Cause i can't add infinite workers x) ?
     
  18. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Just increase the async-workers setting in pocketmine.yml by one or two.
     
  19. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    If i increase it what it will do for my servers performance ?
    Cause i have 6 servers on a VPS and i don't want them to be laggy
     
  20. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    If you have too many worker threads but you don't use that many worker threads, it will waste your CPU. It should generally be the number of cores your server has, but if you use it for running thread-blocking tasks, it's OK to increase it by one or two depending on how frequently you schedule the async tasks.
    If you have too few workers, it ends up executing AsyncTasks too slowly because it spends all the time waiting or some CPUs are not utilized.
    If you have too many workers, some workers are running doing nothing. This will waste your CPU.

    I have been discussing/debating this with @dktapps for two days and I haven't drawn a good conclusion yet :p
     
    Miste and dktapps like this.
  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.