Help using Scheduler in python (I almost got it...)

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Post Reply
Zak
Posts: 13
Joined: 04 Mar 2008, 07:41
Location: Las Vegas
Contact:

Help using Scheduler in python (I almost got it...)

Post by Zak »

Hello,

I have tried to port over the code in "8.18 Server side scheduler" to python, and came very close, but can't get past a certain part.

I can't make a ITaskHandler class... it gives me error:

Traceback (innermost last):
File "<string>", line 863, in init
TypeError: can't instantiate interface (it.gotoandplay.smartfoxserver.util.scheduling.ITaskHandler)
08:45:51.892 - [ WARNING ] > Error in extension [ game.py ]: Traceback (innermost last):
File "<string>", line 863, in init
TypeError: can't instantiate interface (it.gotoandplay.smartfoxserver.util.scheduling.ITaskHandler)
--- At line: 70



For others trying to do the same thing, you can compare my code with the original 8.18 actionscript code in the documentation.

Here's my whole code:



Code: Select all

from it.gotoandplay.smartfoxserver.util.scheduling import Scheduler 
from it.gotoandplay.smartfoxserver.util.scheduling import Task
from it.gotoandplay.smartfoxserver.util.scheduling import ITaskHandler

scheduler = None   #null won't work in python.


def init():

                global scheduler

                scheduler = Scheduler();
      
      
                task1  = Task( {"name":"have breakfast"} )



                handlerObj = {};
                handlerObj["count"] = 0
                handlerObj["doTask"] = timerTest;   #referring to a function below since python can't use inline functions...
                
              ##########################
              #this is the code where I get the error:
                taskHandler = ITaskHandler( handlerObj )
              ##########################

                scheduler.addScheduledTask(task1, 3, false, taskHandler)


and finally outside of init, I made another function called timerTest to correspond with what doTask is pointing to :

Code: Select all

def timerTest(task):
                _server.trace("timer test callback called");




Any help would be appreciated. If I can just get past this one sticking point then I should be able to finish my project and I will post up a detailed walk through to help others in the future.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

In order to create an ITaskHandler you should create a class that implements that interface.

"Implementing an interface" in Java means creating the concrete implementation of the methods declared in the interface in a new class.

You can do the same in Python like this:

Code: Select all

class MyTaskHandler(ITaskHandler):
	def doTask(self, task):
		# do something cool here
		pass
		
handler = MyTaskHandler()
( disclaimer: just wrote the code without testing it, should work :) )
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

the reason why you add the doTask() method is because that's the only method declared by the interface --> http://www.smartfoxserver.com/docs/docP ... ndler.html
Lapo
--
gotoAndPlay()
...addicted to flash games
CB
Posts: 45
Joined: 04 Aug 2006, 11:15

Python Scheduler

Post by CB »

Hey Zak -

Did you get this to wrk> Could you post the final code if you did? I've been trying the same thing and am still getting an error.

Thanks

C
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

which error?
Lapo
--
gotoAndPlay()
...addicted to flash games
CB
Posts: 45
Joined: 04 Aug 2006, 11:15

Post by CB »

found answer here:





regarding destroy.scheduler(null) and error in example.

Thanks

C
Zak
Posts: 13
Joined: 04 Mar 2008, 07:41
Location: Las Vegas
Contact:

Scheduler in python, how to:

Post by Zak »

Sorry it took so long, but we were working on other aspects of the poker game that I am building. It is done now, and I am adding in the scheduler to handle server based timing (new hand, count down each player turn, etc..)


Here's a working python version of scheduler:

Outside init:

Code: Select all

from it.gotoandplay.smartfoxserver.util.scheduling import Scheduler 
from it.gotoandplay.smartfoxserver.util.scheduling import Task
from it.gotoandplay.smartfoxserver.util.scheduling import ITaskHandler

scheduler = None;  #None is the same as null in python.

in init:

Code: Select all

global scheduler;
scheduler = Scheduler();
scheduler.startService();
in destroy:

Code: Select all

scheduler.destroy(None)    #or else you'll get tasks still firing off even if you reload the extension, needing server restart to stop it.

Next I am using it out of a custom class, Game, so I create a handler for it, outside the Game class:

Code: Select all

class GameTaskHandler(ITaskHandler): 
    def __init__(self, handlerObj, _server):
        self._server = _server;
        self.handlerObj = handlerObj;
    def doTask(self, task): 
        # do something cool here 
        self._server.trace("in doTask: " + str(task.id['name']));
        self.handlerObj["doTask"]();
        pass 
I passed it the _server instance so I can trace output if I need to.

and I can setup and stop tasks within the Game class I made:

in __init__ in the Game class:

Code: Select all

self.scheduler = scheduler;   #passed in via constructor.

and finally when I need to use the scheduler from within my Game class:

Code: Select all

self.taskNewGame  = Task( {"name":"start new game"} )
            handlerObj = {};
            handlerObj["count"] = 0
            handlerObj["roomId"] = self.roomId;
            handlerObj["doTask"] = self.resetGame;
     
            self.newGameHandler = GameTaskHandler(handlerObj, self._server)
                
            self.scheduler.addScheduledTask(self.taskNewGame, 8, False, self.newGameHandler);  #start a new game after 8 seconds, and only do it once.

self.resetGame is a method within the Game class, and roomId, a property passed at construction to the Game class (each game is a different room, except for the lobby).

If I want, I can pass a dictionary of arguments (the handlerObj) as well, to pass to the resetGame method.

if there is a task that I need to disable before it fires off, I can just do something like: self.whateverTask.active = False;



I have tested it using several Game classes (rooms) running simultaneously, and it works fine. If I start getting conflicts (alot of Game classes using the same scheduler, I can probably create a list or dictionary of scheduler instances, and make sure that they are all destroyed properly from the destroy funciton.

I hope this helps anybody.
CB
Posts: 45
Joined: 04 Aug 2006, 11:15

Post by CB »

nice! It does.

Good luck!

C
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Thanks Zak! :)
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply