Using Asyncore/chat for game server question.
Jos
hyakugei at gmail.com
Thu Oct 6 00:01:39 EDT 2005
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>
More information about the Python-list
mailing list