Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38db47c
commit 8ec780a
Showing
8 changed files
with
101 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import os | ||
import yaml | ||
import gevent | ||
import unittest | ||
import uuid | ||
import time | ||
|
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
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 |
---|---|---|
@@ -1,16 +1,48 @@ | ||
import weakref | ||
import threading | ||
from multiprocessing.pool import ThreadPool | ||
""" | ||
Helper functions for working with stdlib threading library | ||
Inspired by the gevent api | ||
""" | ||
|
||
def spawn(func, *args, **kwargs): | ||
_thread = threading.Thread(target=func, args=tuple(args), kwargs=kwargs) | ||
if hasattr(func, 'im_class'): | ||
name = "%s.%s" % (func.im_class.__name__, func.__name__) | ||
else: | ||
name = func.__name__ | ||
|
||
_thread = threading.Thread(target=func, name=name, args=tuple(args), kwargs=kwargs) | ||
_thread.daemon = True | ||
_thread.start() | ||
return _thread | ||
|
||
|
||
def joinall(threads): | ||
for thread in threads: | ||
thread.join() | ||
|
||
class Pool(ThreadPool): | ||
"""Subclass multiprocessing's ThreadPool to have a similar API to gevent | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
"""We need to patch threading to support ThreadPool being run in | ||
child threads. | ||
Shouldn't be necessary when http://bugs.python.org/issue10015 is fixed | ||
""" | ||
|
||
if not hasattr(threading.current_thread(), "_children"): | ||
threading.current_thread()._children = weakref.WeakKeyDictionary() | ||
|
||
ThreadPool.__init__(self, *args, **kwargs) | ||
|
||
def spawn(self, func, *args, **kwargs): | ||
|
||
self.apply_async(func, tuple(args), kwargs) | ||
|
||
def join(self): | ||
self.close() | ||
ThreadPool.join(self) |