Using Asyncore/chat for game server question.

J

Jos

Hello all.

I have a working server, using the asyncore/chat module, which enables
logging in, rooms and private messaging.

I've used this framework to make some simple games that only required
message broadcasting via the server.

I'd now like to move the game logic into the server.

My recent reading includes the article "Multithreaded Game Scripting
with Stackless Python"

http://harkal.sylphis3d.com/2005/08/10/multithreaded-game-scripting-with-stackless-python/

I'm not so interested in moving to Stackless, although i've also been
looking at Nanothreads offered in the "Lightweight Game Toolkit" as a
way to implement the above ideas in plain python.

http://lgt.berlios.de/#nanothreads

However, that's a bit off from my Question. So here is is:

How do i enable the/a game object, running on the server, to send
messages "on its own". I understand that the asyncore module is based
on the "Reactor" design, which is, duh, reactive rather then active...
So how do i make my server "active" ;).

A simple example would be a server based on asyncore, with multiple
connections, which broadcasts a "pulse" to the connected clients every
second.

Thanks for any help, pointers, words of advice, etc etc!

Jos
 
J

Jos

Thanks to Matt Hammond and Jonathan LaCour for getting back to me!

Jonathan sent me some code which took me several days to figure out,
but does _exactly_ what i wanted.

You can find the code he sent (dug out of the medusa distro) here:
http://www.thethirdplace.org/grabbag/pyasynccpl/select_trigger.py

And how i warped it to my needs here:
http://www.thethirdplace.org/grabbag/pyasynccpl/test.py
(please don't laugh at the rough code! :)

Jonathan's original reply, via email, is quoted here with his
permission.

Thanks!
jos

<quote>
I saw your post to comp.lang.python about asyncore and making a bit
more of an "active" process. The problem with asyncore, as you stated,
is that it "reacts" to events on file descriptors (through
select/poll). If you have something that occurs in a second thread
that could cause data to be available (ie, sending a "pulse" every
second), you can use some old code from the medusa web server to do
this, which I will attached to this email.

You will need to do something like this (I haven't tested this code):

from select_trigger import trigger
from threading import Thread
import time

class pulse_thread(Thread):
def __init__(self, interval, thunk):
Thread.__init__(self)
self.trigger = trigger()
self.thunk = thunk
self.interval = interval

def run(self):
while True:
self.trigger.pull_trigger(self.thunk)
time.sleep(self.interval)

The pulse thread will pull the trigger how ever often you like, thus
waking up the asyncore loop for events being fired. I hope this helps.

-- Jonathan
</quote>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top