-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Builtin thread-local object storage for AsyncTask #1
Conversation
#blamedktapps |
Update AsyncTask.php Fix syntax error
Rebased commits |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome :)
@@ -27,7 +27,7 @@ | |||
/** | |||
* Class used to run async tasks in other threads. | |||
* | |||
* WARNING: Do not call PocketMine-MP API methods, or save objects from/on other Threads!! | |||
* WARNING: Do not call PocketMine-MP API methods, or save objects (and arrays cotaining objects) from/on other Threads!! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love this :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lol hilarious
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@remotevase please stop spamming on our repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, but expect to see more pull requests from me that actually contribute CODE not just templates for issues and pull requests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@remotevase Please immediately stop using pull requests and issues as chatroom or forums, or respective action will be taken.
I could try to test this, if you want |
The commit name seems to be not very descriptive |
@robske110 that's all that this pull request is about. Other changes are just to assist this addition. You don't want to store a value; you just want to retrieve a value, and you do so by storing it first, along with other things like a storage to put these values at. But the main point is that you can use At least, I think it is good enough just for a commit name to identify a change. Better than this 😉 |
well, still looks weird for me, but i guess ok |
Tested with this script plugin: <?php
/**
* @name AsyncTest
* @author SOFe
* @main AsyncTest\AsyncTest
* @api 2.1.0
* @version 1.0.0
*/
namespace AsyncTest;
class AsyncTest extends \pocketmine\plugin\PluginBase {
public function onEnable(){
$asyncTask = new class($this) extends \pocketmine\scheduler\AsyncTask{
public function onRun(){
sleep(5);
}
public function onCompletion(\pocketmine\Server $server){
$server->getLogger()->info('$asyncTask');
var_dump(get_class($this->fetchLocal($server)));
}
};
$this->getServer()->getScheduler()->scheduleAsyncTask($asyncTask);
$asyncTask2 = new class($this) extends \pocketmine\scheduler\AsyncTask{
public function onRun(){
sleep(10);
}
public function onCompletion(\pocketmine\Server $server){
$server->getLogger()->info('$asyncTask2');
// var_dump(get_class($this->fetchLocal($server)));
}
};
$this->getServer()->getScheduler()->scheduleAsyncTask($asyncTask2);
}
} Note that anonymous classes are used in this script, and the values to store for an AsyncTask is the Output with [12:07:07] [Server thread/INFO]: Enabling AsyncTest v1.0.0
# Irrelevant lines skipped
[12:07:07] [Server thread/INFO]: Done (1.414s)! For help, type "help" or "?"
[12:07:12] [Server thread/INFO]: $asyncTask
~\plugins\async.php:22:
string(19) "AsyncTest\AsyncTest"
[12:07:22] [Server thread/INFO]: $asyncTask2
[12:07:22] [Server thread/DEBUG]: AsyncTask class@anonymous~\plugins\async.php0000006F77B8036A stored local complex data but did not remove them after completion |
*/ | ||
public function removeLocalComplex(AsyncTask $for) : bool{ | ||
if(isset($this->objectStore[$for])){ | ||
Server::getInstance()->getLogger()->debug("AsyncTask " . get_class($for) . " stored local complex data but did not remove them after completion"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Should this be at the WARN level instead?
The pull request is ready for merge. Apart from the question about WARN vs DEBUG above, it is OK for merging. Please review this pull request if you find no problems in the API design. |
I think NOTICE would be the right level. |
Changed log level of potential memory leak from DEBUG to NOTICE. This is really wrong. It should be shown even on production servers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good now, I'd say ready for merge 👍
* Constructs a new instance of AsyncTask. Subclasses don't need to call this constructor unless an argument is to be passed. ONLY construct this class from the main thread. | ||
* <br> | ||
* If an argument is passed into this constructor, it will be stored in a thread-local storage (in ServerScheduler), which MUST be retrieved through {@link #fetchLocal} when {@link #onCompletion} is called. | ||
* Otherwise, a DEBUG level message will be raised and the reference will be removed after onCompletion exits. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't you change this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed in detail.
@dktapps deleted my original pull request. Blame him and ask him for description.