本文整理汇总了Python中gevent.threadpool.ThreadPool.apply方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadPool.apply方法的具体用法?Python ThreadPool.apply怎么用?Python ThreadPool.apply使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gevent.threadpool.ThreadPool
的用法示例。
在下文中一共展示了ThreadPool.apply方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: bench_apply
# 需要导入模块: from gevent.threadpool import ThreadPool [as 别名]
# 或者: from gevent.threadpool.ThreadPool import apply [as 别名]
def bench_apply(loops):
pool = ThreadPool(1)
t0 = perf.perf_counter()
for _ in xrange(loops):
for _ in xrange(N):
pool.apply(noop)
pool.join()
pool.kill()
return perf.perf_counter() - t0
示例2: PortDao
# 需要导入模块: from gevent.threadpool import ThreadPool [as 别名]
# 或者: from gevent.threadpool.ThreadPool import apply [as 别名]
class PortDao(object):
def __init__(self, config_file):
self.conf_file = STRING(config_file)
self._threadpool = ThreadPool(1)
self._cached_port_conf_list = []
self._load_conf()
def _load_conf(self):
if os.path.isfile(self.conf_file):
config = self._threadpool.apply(
Config.from_file,
(self.conf_file, conf_file_schema))
self._cached_port_conf_list = config.conf
def get_port_conf_list(self):
return copy.deepcopy(self._cached_port_conf_list)
def get_port_conf(self, port_name):
for port_config in self._cached_port_conf_list:
if port_config.get("port_name") == port_name:
break
else:
raise StreamSwitchError("Port (%s) Not Exist in config file(%)" %
(port_name, self.self_conf_file), 404)
return copy.deepcopy(port_config)
def update_port_conf(self, port_name, new_port_config):
new_port_config = new_port_config_schema.validate(new_port_config)
for port_config in self._cached_port_conf_list:
if port_config.get("port_name") == port_name:
break
else:
raise StreamSwitchError("Port (%s) Not Exist in config file(%)" %
(port_name, self.self_conf_file), 404)
port_config.update(new_port_config)
save_port_conf_list = copy.deepcopy(self._cached_port_conf_list)
self._threadpool.apply(Config.to_file,
(self.conf_file, save_port_conf_list))
示例3: BaseReader
# 需要导入模块: from gevent.threadpool import ThreadPool [as 别名]
# 或者: from gevent.threadpool.ThreadPool import apply [as 别名]
class BaseReader(c_void_p):
def __str__(self):
return '<%X>' % (self.value if self.value else -1)
__repr__ = __str__
def __init__(self,path = None,baud = None,parity = None,impl = None,
explicit_error = False):
'''
Reader object can be created even if required port cannot be opened.
It will try to fix itself afterwards.
To check current Reader status use 'is_open' method.
'''
self.pool = ThreadPool(1)
self._is_open = False
if not path:
kw = config.reader_path[0]
path,baud,parity,impl = (kw['path'],kw.get('baud',DEFAULT_BAUD),
kw.get('parity',DEFAULT_PARITY),
kw.get('impl',config.default_impl))
self.path = path
self.baud = baud if baud != None else DEFAULT_BAUD
self.parity = parity if parity != None else DEFAULT_PARITY
self.impl = impl if impl != None else config.default_impl
try:
self.open()
except ReaderError:
if explicit_error: raise
print 'Cannot open Reader on {0}. Will try to fix afterwards...'.format(self.path)
def is_open(self):
return self._is_open
@staticmethod
def execute_with_context(context, callback, args, kwds):
with context:
return callback(*args, **kwds)
def apply(self, callback, args = None, kwds = None):
if args == None:
args = ()
if kwds == None:
kwds = {}
return self.pool.apply(self.execute_with_context, args = (self,callback,args,kwds))
def __enter__(self):
self.exc_info = (None,None,None)
return self
def __exit__(self, type, value, traceback):
self.exc_info = (type,value,traceback)
return True
def open(self):
'Opens reader on a given port and raises ReaderError otherwise.'
if DEBUG: print 'Reader.open',(self.path,self.baud,self.parity,self.impl)
if not self._is_open:
if reader_open(self.path,self.baud,self.parity,self.impl,self): raise ReaderError()
self._is_open = True
def close(self):
'Closes current reader connection if it was open before.'
if self._is_open:
reader_close(self)
self._is_open = False
def reopen(self):
print 'reopen'
self.close()
self.open()