Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HH-15804 naive threading for xslt impl
spawning for new thread on each xslt = there is no thread pool
- Loading branch information
pavel trukhanov
committed
Oct 19, 2010
1 parent
3ebec2d
commit 008afd2
Showing
3 changed files
with
84 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import time | ||
import functools | ||
import threading | ||
import tornado.ioloop | ||
io_loop = tornado.ioloop.IOLoop.instance() | ||
|
||
import logging | ||
log = logging.getLogger('frontik.jobs') | ||
|
||
class _Job(threading.Thread): | ||
def __init__(self, func, done): | ||
threading.Thread.__init__(self) | ||
self.func = func | ||
self.result = None | ||
self.done = done | ||
|
||
def run(self): | ||
self.result = self.func() | ||
self.done.set() | ||
|
||
class _Executor(): | ||
def __init__(self): | ||
self.events = [] | ||
|
||
def start_job(self, func, cb): | ||
done = threading.Event() | ||
job = _Job(func, done) | ||
def _cb(): | ||
cb(job.result) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
job.start() | ||
self.events.append((done, _cb)) | ||
self.listen_events() | ||
|
||
def listen_events(self): | ||
# log.debug('active threads count = ' + str(threading.active_count())) | ||
ev_c = len(self.events) | ||
# log.debug('waiting events count = ' + str(ev_c)) | ||
if ev_c != 0: | ||
# io_loop.add_callback(self._event_listener) | ||
io_loop.add_timeout(time.time()+0.001, self._event_listener) | ||
|
||
def _event_listener(self): | ||
undone_events = filter(lambda (e, cb): not e.is_set(), self.events) | ||
if undone_events.count != self.events.count: | ||
done_events = filter(lambda (e, cb): e.is_set(), self.events) | ||
self.events = undone_events | ||
map(lambda (e, cb): cb(), done_events) | ||
self.listen_events() | ||
This comment has been minimized.
Sorry, something went wrong.
elephantum
Contributor
|
||
|
||
executor = _Executor() |
1 comment
on commit 008afd2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не мудри, выкинь машинерию с jobs сейчас.
Сделай просто:
threading.Thread(target=apply_xslt_and_return_control_to_ioloop).start()
тебе больше ничего не нужно.
Вот это не правильно. Весь код работающий в ioloop не thread-safe, поэтому нужно не на прямую запускать cb, а передавать управление в ioloop.