本文整理汇总了Python中twisted.internet.task.LoopingCall.reset方法的典型用法代码示例。如果您正苦于以下问题:Python LoopingCall.reset方法的具体用法?Python LoopingCall.reset怎么用?Python LoopingCall.reset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.task.LoopingCall
的用法示例。
在下文中一共展示了LoopingCall.reset方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Consumer
# 需要导入模块: from twisted.internet.task import LoopingCall [as 别名]
# 或者: from twisted.internet.task.LoopingCall import reset [as 别名]
class Consumer(object):
"""A simple Kafka consumer implementation
This consumer consumes a single partition from a single topic, optionally
automatically committing offsets. Use it as follows:
- Create an instance of :class:`afkak.KafkaClient` with cluster
connectivity details.
- Create the :class:`Consumer`, supplying the client, topic, partition,
processor function, and optionally fetch specifics, a consumer group,
and a commit policy.
- Call :meth:`.start` with the offset within the partition at which to
start consuming messages. See :meth:`.start` for details.
- Process the messages in your :attr:`.processor` callback, returning a
:class:`~twisted.internet.defer.Deferred` to provide backpressure as
needed.
- Once processing resolves, :attr:`.processor` will be called again with
the next batch of messages.
- When desired, call :meth:`.stop` on the :class:`Consumer` to halt
calls to the :attr:`processor` function and cancel any outstanding
requests to the Kafka cluster.
A :class:`Consumer` may be restarted once stopped.
:ivar client:
Connected :class:`KafkaClient` for submitting requests to the Kafka
cluster.
:ivar bytes topic:
The topic from which to consume messages.
:ivar int partition:
The partition from which to consume.
:ivar callable processor:
The callback function to which the consumer and lists of messages
(:class:`afkak.common.SourcedMessage`) will be submitted
for processing. The function may return
a :class:`~twisted.internet.defer.Deferred` and will not be called
again until this Deferred resolves.
:ivar bytes consumer_group:
Optional consumer group ID for committing offsets of processed
messages back to Kafka.
:ivar bytes commit_metadata:
Optional metadata to store with offsets commit.
:ivar int auto_commit_every_n:
Number of messages after which the consumer will automatically
commit the offset of the last processed message to Kafka. Zero
disables, defaulted to :data:`AUTO_COMMIT_MSG_COUNT`.
:ivar int auto_commit_every_ms:
Time interval in milliseconds after which the consumer will
automatically commit the offset of the last processed message to
Kafka. Zero disables, defaulted to :data:`AUTO_COMMIT_INTERVAL`.
:ivar int fetch_size_bytes:
Number of bytes to request in a :class:`FetchRequest`. Kafka will
defer fulfilling the request until at least this many bytes can be
returned.
:ivar int fetch_max_wait_time:
Max number of milliseconds the server should wait for that many
bytes.
:ivar int buffer_size:
default 128K. Initial number of bytes to tell Kafka we have
available. This will be raised x16 up to 1MB then double up to...
:ivar int max_buffer_size:
Max number of bytes to tell Kafka we have available. `None` means
no limit (the default). Must be larger than the largest message we
will find in our topic/partitions.
:ivar float request_retry_init_delay:
Number of seconds to wait before retrying a failed request to
Kafka.
:ivar float request_retry_max_delay:
Maximum number of seconds to wait before retrying a failed request
to Kafka (the delay is increased on each failure and reset to the
initial delay upon success).
:ivar int request_retry_max_attempts:
Maximum number of attempts to make for any request. Default of zero
means retry forever; other values must be positive and indicate
the number of attempts to make before returning failure.
:ivar int auto_offset_reset:
What action should be taken when the broker responds to a fetch request
with `OffsetOutOfRangeError`?
- `OFFSET_EARLIEST`: request the oldest available messages. The
consumer will read every message in the topic.
- `OFFSET_LATEST`: request the most recent messages (this is the Java
consumer's default). The consumer will read messages once new
messages are produced to the topic.
- `None`: fail on `OffsetOutOfRangeError` (Afkak's default). The
`Deferred` returned by :meth:`Producer.start()` will errback. The caller
may call :meth:`~.start()` again with the desired offset.
The broker returns `OffsetOutOfRangeError` when the client requests an
offset that isn't valid. This may mean that the requested offset no
longer exists, e.g. if it was removed due to age.
"""
def __init__(self, client, topic, partition, processor,
consumer_group=None,
commit_metadata=None,
auto_commit_every_n=None,
auto_commit_every_ms=None,
fetch_size_bytes=FETCH_MIN_BYTES,
fetch_max_wait_time=FETCH_MAX_WAIT_TIME,
#.........这里部分代码省略.........
示例2: IRCProtocol
# 需要导入模块: from twisted.internet.task import LoopingCall [as 别名]
# 或者: from twisted.internet.task.LoopingCall import reset [as 别名]
class IRCProtocol(irc.IRC):
UNREGISTERED_COMMANDS = ["PASS", "USER", "NICK", "PING", "PONG", "QUIT", "CAP"]
def __init__(self, *args, **kwargs):
self.dead = False
self.type = None
self.password = None
self.nick = None
self.user = None
self.secure = False
self.data = 0
self.data_checker = LoopingCall(self.checkData)
self.pinger = LoopingCall(self.ping)
self.last_message = None
def connectionMade(self):
self.secure = ISSLTransport(self.transport, None) is not None
self.data_checker.start(5)
self.last_message = now()
self.pinger.start(self.factory.client_ping_interval)
ip = self.transport.getPeer().host
expired = []
for mask, linedata in self.factory.xlines["Z"].iteritems():
if linedata["duration"] != 0 and epoch(now()) > epoch(linedata["created"]) + linedata["duration"]:
expired.append(mask)
continue
if fnmatch.fnmatch(ip, mask):
self.sendMessage(
"NOTICE", "*", ":{}".format(self.factory.client_ban_msg), prefix=self.factory.server_name
)
self.sendMessage("ERROR", ":Closing Link {} [Z:Lined: {}]".format(ip, linedata["reason"]))
self.transport.loseConnection()
for mask in expired:
del self.factory.xlines["Z"][mask]
if expired:
self.factory.save_options()
def dataReceived(self, data):
if self.dead:
return
# self.factory.stats_data["bytes_in"] += len(data)
# self.factory.stats_data["total_bytes_in"] += len(data)
self.data += len(data)
self.last_message = now()
if self.pinger.running:
self.pinger.reset()
irc.IRC.dataReceived(self, data)
def checkData(self):
if self.type:
self.type.checkData(self.data)
self.data = 0
def ping(self):
if (now() - self.last_message).total_seconds() > self.factory.client_timeout_delay:
self.transport.loseConnection()
self.connectionLost(None)
else:
self.sendMessage("PING", ":{}".format(self.factory.server_name))
def handleCommand(self, command, prefix, params):
# self.factory.stats_data["lines_in"] += 1
# self.factory.stats_data["total_lines_in"] += 1
log.msg("handleCommand: {!r} {!r} {!r}".format(command, prefix, params))
if not self.type and command not in self.UNREGISTERED_COMMANDS:
return self.sendMessage(
irc.ERR_NOTREGISTERED, command, ":You have not registered", prefix=self.factory.server_name
)
elif not self.type:
return irc.IRC.handleCommand(self, command, prefix, params)
else:
return self.type.handleCommand(command, prefix, params)
def sendLine(self, line):
if self.dead:
return
# self.factory.stats_data["lines_out"] += 1
# self.factory.stats_data["total_lines_out"] += 1
# self.factory.stats_data["bytes_out"] += len(line)+2
# self.factory.stats_data["total_bytes_out"] += len(line)+2
log.msg("sendLine: {!r}".format(line))
return irc.IRC.sendLine(self, line)
def irc_PASS(self, prefix, params):
if not params:
return self.sendMessage(
irc.ERR_NEEDMOREPARAMS, "PASS", ":Not enough parameters", prefix=self.factory.server_name
)
self.password = params[0]
def irc_NICK(self, prefix, params):
if not params:
self.sendMessage(irc.ERR_NONICKNAMEGIVEN, ":No nickname given", prefix=self.factory.server_name)
elif not VALID_USERNAME.match(params[0]):
self.sendMessage(
irc.ERR_ERRONEUSNICKNAME, params[0], ":Erroneous nickname", prefix=self.factory.server_name
)
elif params[0] in self.factory.users:
self.sendMessage(
irc.ERR_NICKNAMEINUSE,
#.........这里部分代码省略.........
示例3: TelldusIn
# 需要导入模块: from twisted.internet.task import LoopingCall [as 别名]
# 或者: from twisted.internet.task.LoopingCall import reset [as 别名]
class TelldusIn(Protocol):
''' Class for incoming Telldus events '''
noisy = False
path = '/tmp/TelldusEvents'
idleTimeout = 60
def __init__(self, parent):
self.log = Logger(namespace=parent.name+'/in')
self.parent = parent
self.master = parent.master
self.status = ColorState(log=self.log, state_format={0:0}) # <-- a hack to avoid color
self.status.add_callback(self.parent.update_status)
self.connected = False
self.factory = TelldusInFactory(self, self.parent)
def connect(self):
self.status.set_OFF()
self.master.reactor.connectUNIX(self.path, self.factory)
def connectionMade(self):
self.log.info("Connected to {p}", p=self.path)
self.connected = True
self.status.set_YELLOW('IN connection made, waiting for data')
self.data = ''
self.elements = []
self.timer = LoopingCall(self.dataTimeout)
self.timer.start(self.idleTimeout, now=False)
def connectionLost(self, reason): # pylint: disable=W0222
self.connected = False
self.log.info("Lost connection with {p}: {e}", p=self.path, e=reason.getErrorMessage())
self.status.set_OFF('IN connection closed')
if self.timer.running:
self.timer.stop()
def disconnect(self):
if self.connected:
self.transport.loseConnection()
def dataReceived(self, data):
self.log.debug('{_rawin}', rawin=data)
if self.timer.running:
self.timer.reset()
else:
self.timer.start(self.idleTimeout, now=False)
data = self.data + data
# Interpret the data
(elements, data) = parsestream(data)
(events, elements) = parseelements(elements, log=self.log)
# Save remaining data for next call (incomplete frame received)
self.data = data
self.elements = elements
# At this point, we can consider the connection up
self.status.set_GREEN()
# Iterate over the received events
for event in events:
self.log.debug('{_datain}', datain=event)
self.parent.parse_event(event)
def dataTimeout(self):
self.timer.stop()
self.log.info("No telldus activity. No connection?")
self.status.set_YELLOW('No activity')
示例4: C2SServerProtocol
# 需要导入模块: from twisted.internet.task import LoopingCall [as 别名]
# 或者: from twisted.internet.task.LoopingCall import reset [as 别名]
class C2SServerProtocol(InternalServerProtocol):
pinger = None
ping_txid = None
def __init__(self, config):
txprotobuf.Protocol.__init__(self, c2s)
self.MAX_LENGTH = config["server"]["c2s.pack_size_max"]
self.idler = LoopingCall(self.onIdle)
if "reset" not in dir(self.idler):
def reset(self):
try:
self.idler.stop()
except:
pass
try:
self.idler.start(IDLE_DELAY, False)
except:
pass
self.idler.reset = reset
def connectionMade(self):
InternalServerProtocol.connectionMade(self)
"""
TODO this breaks compatibility with Android client < 16
info = InternalServerProtocol.connectionMade(self)
# send serverinfo
r = c2s.ServerInfo()
r.version = info['version']
r.client_protocol = info['client-protocol']
r.fingerprint = info['fingerprint']
r.network = info['network']
r.supports.extend(info['supports'])
self.sendBox(r)
"""
# start idler
self.idler.start(IDLE_DELAY, False)
def connectionLost(self, reason=connectionDone):
try:
# stop idler
self.idler.stop()
except:
pass
try:
# stop pinger
self.pinger.stop()
except:
pass
InternalServerProtocol.connectionLost(self, reason)
def onIdle(self):
self.service.idle()
# send ping
a = c2s.Ping()
a.timestamp = long(time.time())
self.ping_txid = self.sendBox(a)
# ping is sent, wait for response
if not self.pinger:
self.pinger = reactor.callLater(PING_DELAY, self.pingTimeout)
def pingTimeout(self):
try:
self.idler.stop()
except:
pass
self.service.ping_timeout()
self.pinger = None
def boxReceived(self, data, tx_id=None):
# reset idler
try:
self.idler.reset()
except:
pass
# optional reply
r = None
name = data.__class__.__name__
if name == "LoginRequest":
r = c2s.LoginResponse()
(r.status, userid) = self.service.login(
str(tx_id), data.token, data.client_protocol, data.client_version, data.flags
)
if userid:
r.user_id = userid
elif name == "AuthenticateRequest":
r = c2s.AuthenticateResponse()
r.valid = self.service.authenticate(str(tx_id), data.token)
elif name == "MessagePostRequest":
if self.service.is_logged():
r = c2s.MessagePostResponse()
if len(data.recipient) > 0:
res = self.service.post_message(
str(tx_id), tuple(data.recipient), str(data.mime), tuple(data.flags), data.content
)
#.........这里部分代码省略.........