Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

AsyncTask and onRun() method...

Comments in 'Plugin Development' started by blocksandgold, Jun 28, 2014.

  1. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    I've created an Async extended class to be called by scheduleAsyncTask method.

    But on the onRun() method i can't retreive any object from my plugin ? Even if I give the plugin reference in the contructor.
    Even static method are not found and i get an autoload error.
    Is it normal ?
  2. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,707
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Yeah, that wouldn't be thread safe :)
    Pass them in the constructor to the AsyncTask and then setResult the output.
    64FF00, blocksandgold and swagboy47 like this.
  3. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    Ok i understand, but is there really no treadsafe methode in the server api ?
    Because i need to do a large number of getBlock in the main level. (To scan a part of the map)
    And i want to do it async to have no impact on the main thread.
    I cannot either log anything with the logger ?
    What is the best way to do it ?
    Is it possible to pass the level to the constructor ? I've done it but it seems to be not allowed to use it after in the OnRun()
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You can serialize the constructor arguments.
    64FF00 and Dinokiller like this.
  5. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    149 likes to you.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Also, don't call API functions from other threads. It is said on the documentation comment, and Level::getBlock() is an API function.
    64FF00 likes this.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Give them to @shoghicp.
    64FF00 likes this.
  8. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    Ok that's clear No API Call from the OnRun()
    I will do all my getBlock() in the main thread. But limit the number in each call.
    thx
  9. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    But look at this (from the pocketmine source code)

    PHP:
    namespace pocketmine\scheduler;

    use 
    pocketmine\Server;
    use 
    pocketmine\utils\Utils;

    class 
    SendUsageTask extends AsyncTask{

       public 
    $endpoint;
       public 
    $data;

       public function 
    __construct($endpoint, array $data){
         
    $this->endpoint $endpoint;
         
    $this->data serialize($data);
       }

       public function 
    onRun(){
         
    Utils::postURL($this->endpointunserialize($this->data));
       }

       public function 
    onCompletion(Server $server){

       }

    }
    The Utils::postURL() is in the API ?
    Why this one is allowed ?
  10. jython234
    Offline

    jython234 Notable Member Plugin Developer

    Joined:
    Nov 4, 2013
    Posts:
    324
    Plugins:
    1
    Minecraft User:
    jython234
    Utils is not in the API.
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Not exactly. What he meant about the API is, usually API calls would just add delayed operations or pass objects. The above utils call only calls a function that is in fact a shortcut for several PHP native functions.
    64FF00 and blocksandgold like this.
  12. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    Ok I understand, your mean there is no hook for this Util Method in the main thread ?
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You can understand my meaning as "this Utils::postURL() method is thread-save".
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    This is the time when we come to think this:
    First, the function should not refer to any static properties in other threads, including the main thread.
    Second, the function should only call [other functions that ultimately only call] native PHP functions.
  15. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    Or Synchronized datas...
  16. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    I'm trying to finish my plugin, but i have issues... :(

    How to debug the onRun() method while I can't call logger method (because it's API method) ?
    I've tried to write on a file, but i've a segmentation fault without any debug trace :/

    What is the purpose of the setResult / getResult / HasResult method ? Is it the only was to return a result from the onRun() method ans read it on the OnCompletion() method ?
    Last edited: Jun 30, 2014
  17. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,707
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Try using print(), you should be able to write to files without issues (I have used that to debug). Set result is so the task can output something to be collected by the plugin (if a result is set the task won't be removed from memory until getResult() is called). Just read AsyncTask.php, it's pretty straightforward.
    64FF00 and blocksandgold like this.
  18. jython234
    Offline

    jython234 Notable Member Plugin Developer

    Joined:
    Nov 4, 2013
    Posts:
    324
    Plugins:
    1
    Minecraft User:
    jython234
    I am like totally never thread safe... :)
    PEMapModder and Falk like this.
  19. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Then go love Java.
    jython234 and 64FF00 like this.
  20. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    print() is ok :) thx !

    My plugin is running now.

    But, I have a last question :

    Why do we need to serialize some parameters

    Here (sendUsage.php), $data is serialized, while $endpoint is not ?

    Is it correct to pass plugin instance to the contructor and to use it on the OnCompletion() methode (which is executed in the main thread) ?

    PHP:
    namespace pocketmine\scheduler;

    use 
    pocketmine\Server;
    use 
    pocketmine\utils\Utils;

    class 
    SendUsageTask extends AsyncTask{

       public 
    $endpoint;
       public 
    $data;

       public function 
    __construct($endpoint, array $data){
         
    $this->endpoint $endpoint;
         
    $this->data serialize($data);
       }

       public function 
    onRun(){
         
    Utils::postURL($this->endpointunserialize($this->data));
       }

       public function 
    onCompletion(Server $server){

       }

    }

Share This Page

Advertisement