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?
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)
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.
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.
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.
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
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.
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.
How to increase the number ? And what it will do if i increase it ? Cause i can't add infinite workers x) ?
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
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