本文整理汇总了Python中twisted.internet.task.LoopingCall类的典型用法代码示例。如果您正苦于以下问题:Python LoopingCall类的具体用法?Python LoopingCall怎么用?Python LoopingCall使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LoopingCall类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add
def add(self, request, save_time = 0):
self.data[request] = time.time() + save_time * 60
d = request.notifyFinish()
d.addCallback(self.notifyFinished, request)
d.addErrback(self.notifyFinished, request)
call = LoopingCall(self.prune)
call.start(60)
示例2: __init__
def __init__(
self, reactor, request, request_rate=10,
sample_size=DEFAULT_SAMPLE_SIZE, timeout=45,
tolerance_percentage=0.2
):
"""
``RequestLoadScenario`` constructor.
:param tolerance_percentage: error percentage in the rate that is
considered valid. For example, if we request a ``request_rate``
of 20, and we give a tolerance_percentage of 0.2 (20%), anything
in [16,20] will be a valid rate.
"""
self.reactor = reactor
self.request = request
self.request_rate = request_rate
self.timeout = timeout
self._maintained = Deferred()
self.rate_measurer = RateMeasurer(sample_size)
self.max_outstanding = 10 * request_rate
self.tolerated_errors = 5 * request_rate
# Send requests per second
self.loop = LoopingCall.withCount(self._request_and_measure)
self.loop.clock = self.reactor
# Monitor the status of the scenario
self.monitor_loop = LoopingCall(self.check_rate)
self.monitor_loop.clock = self.reactor
self.is_started = False
self.rate_tolerated = (
float(request_rate) - (request_rate*tolerance_percentage)
)
示例3: PollingDataStream
class PollingDataStream(DataStream):
""" A self-polling data stream.
This class represents a data stream that wakes up at a given frequency,
and calls the :meth:`poll` method.
"""
frequency = None # Either a timedelta object, or the number of seconds
now = False
def __init__(self):
super(PollingDataStream, self).__init__()
self.timer = LoopingCall(self.poll)
if isinstance(self.frequency, timedelta):
seconds = (
self.frequency.seconds
+ (self.frequency.days * 24 * 60 * 60)
+ (self.frequency.microseconds / 1000000.0)
)
else:
seconds = self.frequency
log.debug("Setting a %s second timer" % seconds)
self.timer.start(seconds, now=self.now)
def poll(self):
raise NotImplementedError
def stop(self):
super(PollingDataStream, self).stop()
try:
if hasattr(self, "timer"):
self.timer.stop()
except Exception, e:
self.log.warn(e)
示例4: __init__
class BandwidthEstimator:
bufsize = 20
totalBytes = 0
def __init__(self, message, length):
self.length = length
self.message = message
self.estim = []
self.bytes = 0
self.call = LoopingCall(self.estimateBandwidth)
self.call.start(1)
def estimateBandwidth(self):
bytes = self.bytes
self.totalBytes += bytes
self.estim.append(bytes)
self.message("%0.2f k/s (%0.2d%%)"
% ((sum(self.estim) / len(self.estim)) / 1024.,
(float(self.totalBytes) / self.length) * 100))
if len(self.estim) > self.bufsize:
self.estim.pop(0)
self.bytes = 0
def stop(self):
self.call.stop()
self.estimateBandwidth()
self.message("Finished receiving: %d bytes (%d%%)" % (
self.totalBytes, (float(self.totalBytes) / self.length) * 100))
示例5: Pinger
class Pinger(object):
"""
An periodic AMP ping helper.
"""
def __init__(self, reactor):
"""
:param IReactorTime reactor: The reactor to use to schedule the pings.
"""
self.reactor = reactor
def start(self, protocol, interval):
"""
Start sending some pings.
:param AMP protocol: The protocol over which to send the pings.
:param timedelta interval: The interval at which to send the pings.
"""
def ping():
protocol.callRemote(NoOp)
self._pinging = LoopingCall(ping)
self._pinging.clock = self.reactor
self._pinging.start(interval.total_seconds(), now=False)
def stop(self):
"""
Stop sending the pings.
"""
self._pinging.stop()
示例6: __init__
def __init__(self):
self.output("TWS init")
self.username = environ["TXTRADER_USERNAME"]
self.password = environ["TXTRADER_PASSWORD"]
self.xmlrpc_port = int(environ["TXTRADER_XMLRPC_PORT"])
self.tcp_port = int(environ["TXTRADER_TCP_PORT"])
self.callback_timeout = int(environ["TXTRADER_TWS_CALLBACK_TIMEOUT"])
if not self.callback_timeout:
self.callback_timeout = DEFAULT_TWS_CALLBACK_TIMEOUT
self.output("callback_timeout=%d" % self.callback_timeout)
self.enable_ticker = bool(int(environ["TXTRADER_ENABLE_TICKER"]))
self.label = "TWS Gateway"
self.channel = "tws"
self.current_account = ""
self.clients = set([])
self.orders = {}
self.pending_orders = {}
self.openorder_callbacks = []
self.accounts = []
self.account_data = {}
self.positions = {}
self.position_callbacks = []
self.executions = {}
self.execution_callbacks = []
self.bardata_callbacks = []
self.cancel_callbacks = []
self.order_callbacks = []
self.addsymbol_callbacks = []
self.accountdata_callbacks = []
self.last_connection_status = ""
self.connection_status = "Initializing"
self.LastError = -1
self.next_order_id = -1
self.last_minute = -1
self.handlers = {
"error": self.error_handler,
"tickSize": self.handle_tick_size,
"tickPrice": self.handle_tick_price,
"tickString": self.handle_tick_string,
"nextValidId": self.handle_next_valid_id,
"currentTime": self.handle_time,
"managedAccounts": self.handle_accounts,
"orderStatus": self.handle_order_status,
"openOrder": self.handle_open_order,
"openOrderEnd": self.handle_open_order_end,
"execDetails": self.handle_exec_details,
"execDetailsEnd": self.handle_exec_details_end,
"position": self.handle_position,
"positionEnd": self.handle_position_end,
"historicalData": self.handle_historical_data,
"updateAccountValue": self.handle_account_value,
"accountDownloadEnd": self.handle_account_download_end,
}
self.ticker_ids = {}
self.symbols = {}
self.symbols_by_id = {}
self.primary_exchange_map = {}
self.tws_conn = None
repeater = LoopingCall(self.EverySecond)
repeater.start(1)
示例7: __init__
def __init__(self, settings, strategy_class):
partition_id = settings.get('SCORING_PARTITION_ID')
if partition_id is None or type(partition_id) != int:
raise AttributeError("Scoring worker partition id isn't set.")
messagebus = load_object(settings.get('MESSAGE_BUS'))
mb = messagebus(settings)
spider_log = mb.spider_log()
scoring_log = mb.scoring_log()
self.consumer = spider_log.consumer(partition_id=partition_id, type=b'sw')
self.scoring_log_producer = scoring_log.producer()
self._manager = FrontierManager.from_settings(settings, strategy_worker=True)
codec_path = settings.get('MESSAGE_BUS_CODEC')
encoder_cls = load_object(codec_path+".Encoder")
decoder_cls = load_object(codec_path+".Decoder")
self._decoder = decoder_cls(self._manager.request_model, self._manager.response_model)
self._encoder = encoder_cls(self._manager.request_model)
self.update_score = UpdateScoreStream(self._encoder, self.scoring_log_producer, 1024)
self.states_context = StatesContext(self._manager.backend.states)
self.consumer_batch_size = settings.get('SPIDER_LOG_CONSUMER_BATCH_SIZE')
self.strategy = strategy_class.from_worker(self._manager, self.update_score, self.states_context)
self.states = self._manager.backend.states
self.stats = {
'consumed_since_start': 0
}
self.job_id = 0
self.task = LoopingCall(self.work)
self._logging_task = LoopingCall(self.log_status)
self._flush_states_task = LoopingCall(self.flush_states)
logger.info("Strategy worker is initialized and consuming partition %d", partition_id)
示例8: reconfigServiceWithBuildbotConfig
def reconfigServiceWithBuildbotConfig(self, new_config):
# first, enable or disable
if new_config.metrics is None:
self.disable()
else:
self.enable()
metrics_config = new_config.metrics
# Start up periodic logging
log_interval = metrics_config.get('log_interval', 60)
if log_interval != self.log_interval:
if self.log_task:
self.log_task.stop()
self.log_task = None
if log_interval:
self.log_task = LoopingCall(self.report)
self.log_task.clock = self._reactor
self.log_task.start(log_interval)
# same for the periodic task
periodic_interval = metrics_config.get('periodic_interval', 10)
if periodic_interval != self.periodic_interval:
if self.periodic_task:
self.periodic_task.stop()
self.periodic_task = None
if periodic_interval:
self.periodic_task = LoopingCall(periodicCheck,
self._reactor)
self.periodic_task.clock = self._reactor
self.periodic_task.start(periodic_interval)
# upcall
return util_service.ReconfigurableServiceMixin.reconfigServiceWithBuildbotConfig(self,
new_config)
示例9: WebSocketConnection
class WebSocketConnection(WebSocketProtocol):
def __init__(self, stateObject):
self.state = stateObject
self.opcode = TEXT
WebSocketProtocol.__init__(self, WebSocketReceiver(self))
self.finished = Deferred()
self.pingLoop = LoopingCall(self.doPing)
def doPing(self):
self.receiver.transport.sendFrame(PING, '')
def write(self, data):
self.receiver.transport.sendFrame(self.opcode, data)
def sendFrame(self, opcode, data):
self.receiver.transport.sendFrame(opcode, data)
def writeSequence(self, data):
for chunk in data:
self.write(chunk)
def connectionLost(self, reason):
if self.pingLoop.running:
self.pingLoop.stop()
self.finished.callback(self)
示例10: __init__
def __init__(self):
"""
Initialize the model. This doesn't add any documents yet.
"""
silenceGensim()
self.dictionaries = dict()
self.preprocessor = TokenizingPorter2Stemmer()
#this dict keeps a model for every source type
# (since e.g. RSS feeds should be treated separately from twitter feeds)
self.models = dict()
#this dict keeps a dictionary for every source type
self.dictionaries = dict()
self.queue = Queue()
self.modelQueue = Queue()
self.nodeCommunicator = NodeCommunicator(self, LISTEN_PORT)
self.nodeCommunicator.registerWithNode(CORE_IP, REGISTER_PORT) # register this node with the core
ln.info("LSA Initialized")
self.updating = False
loop = LoopingCall(self.update)
loop.start(5)
reactor.run()
示例11: CacheService
class CacheService(Service):
def __init__(self, *args, **kwargs):
self.call = None
self.node_updater = NodeCacheUpdater()
self.cluster_updater = ClusterCacheUpdater()
self.vm_updater = VirtualMachineCacheUpdater()
self.job_updater = JobCacheUpdater()
def update_cache(self):
""" a single run of all update classes """
return DeferredList(
[
self.vm_updater.update(),
self.job_updater.update(),
self.node_updater.update(),
self.cluster_updater.update(),
]
)
def startService(self):
self.call = LoopingCall(self.update_cache)
self.call.start(settings.PERIODIC_CACHE_REFRESH)
def stopService(self):
if self.call is not None:
self.call.stop()
示例12: onJoin
def onJoin(self, details):
log.msg("PiMonitor connected")
extra = self.config.extra
self._id = extra['id']
self.publish_temperature = True
self.threshold = 0
self._tick = 0
self._cpu_temp_celsius = None
def scanTemperature():
self._cpu_temp_celsius = float(open("/sys/class/thermal/thermal_zone0/temp").read()) / 1000.
if self.publish_temperature:
self.publish(u'io.crossbar.examples.pi.{}.tempmon.on_temperature'.format(self._id), self._tick, self._cpu_temp_celsius)
self._tick += 1
if self.threshold > 0 and self._cpu_temp_celsius > self.threshold:
self.publish(u'io.crossbar.examples.pi.{}.tempmon.on_threshold_exceeded'.format(self._id), self._tick, self._cpu_temp_celsius)
scan = LoopingCall(scanTemperature)
scan.start(1)
for proc in [self.get_temperature, self.impose_stress, self.toggle_publish, self.set_threshold]:
uri = u'io.crossbar.examples.pi.{}.tempmon.{}'.format(self._id, proc.__name__)
yield self.register(proc, uri)
log.msg("Registered {}".format(uri))
log.msg("PiMonitor ready.")
示例13: __init__
class Hoster:
def __init__(self, screen, port):
self.screen = screen
self.port = port
def start(self):
# Set up the connection between the state and the network;
# using queues gives a degree of seperation between the
# communication and game logic, and making them be deferred
# keeps it all asynchronous.
inqueue = DeferredQueue()
outqueue = DeferredQueue()
#Initialize GameState
gs = GameState(self,self.screen,1,inqueue,outqueue)
#Create Looping Call
self.lc = LoopingCall(gs.gameLoop)
self.lc.start(.0166666666)
#Begin Listening
connfactory = CommandConnFactory(inqueue,outqueue)
self.listening = reactor.listenTCP(self.port,connfactory)
def stop(self, nextscreen=None):
# Stop the GameState logic, and let go of the port on which we're listening
self.lc.stop()
self.listening.stopListening()
# Start up the next screen, if there is one
if nextscreen:
nextscreen.start()
else: reactor.stop()
示例14: delayed_setup
def delayed_setup(self):
self.feeds = []
self.failures.clear()
self.feed_times.clear()
self.targets.clear()
self.tasks.clear()
for name, target in self.config["targets"].items():
proto = target["protocol"]
if proto in self.factory_manager.factories:
self.targets[name] = target
else:
self.logger.warn("Unknown protocol '%s' in target '%s'"
% (proto, name))
for feed in self.config["feeds"]:
append = True
for target in feed["targets"]:
if target["name"] not in self.targets:
self.logger.warn("Unknown target '%s' for feed '%s'"
% (target["name"], feed["name"]))
append = False
break
if append:
self.feeds.append(feed)
for feed in self.feeds:
task = LoopingCall(self.check_feed, feed)
self.tasks[feed["name"]] = task
task.start(feed["frequency"])
示例15: make_lc
def make_lc(self, reactor, func):
if DEBUG:
self.stdout_length = 0
self.stderr_length = 0
def _(lc, reactor):
if DEBUG:
stdout = self.stdout.getvalue()
stderr = self.stderr.getvalue()
if self.stdout.getvalue()[self.stdout_length:]:
print(self.stdout.getvalue()[self.stdout_length:],
file=sys.__stdout__)
if self.stderr.getvalue()[self.stderr_length:]:
print(self.stderr.getvalue()[self.stderr_length:],
file=sys.__stderr__)
self.stdout_length = len(stdout)
self.stderr_length = len(stderr)
return func(lc, reactor)
lc = LoopingCall(_)
lc.a = (lc, reactor)
lc.clock = reactor
lc.start(0.1)
return lc