peerlink/example/tasks.py

49 lines
1.2 KiB
Python
Raw Normal View History

2014-08-28 12:52:31 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
from Queue import Queue
from threading import Thread
from websocket import trigger_event
import state
import link
import logging
logger = logging.getLogger('websocket')
class Tasks(Thread):
_active = True
def __init__(self):
self.q = Queue()
Thread.__init__(self)
self.daemon = True
self.start()
def run(self):
while self._active:
m = self.q.get()
if m:
action, data = m
logger.debug('process task: %s data: %s', action, data)
if action == 'sendMessage':
for peer in state.info.get('nodes', []):
link.remote(peer, 'message', data)
elif action == 'nick':
state.info['nick'] = data
for peer in state.info.get('nodes', []):
link.remote(peer, 'nick', data)
self.q.task_done()
def join(self):
self._active = False
self.q.put(None)
self.q.join()
return Thread.join(self)
def queue(self, action, data=None):
self.q.put((action, data))