sockets - Python's asyncore to periodically send data using a variable timeout. Is there a better way? -
I wanted to write to the server that the client can receive and receive periodic updates without connecting. The problem I have experienced with Escnor is that, if you have a dispatcher You do not return the truth when you write (), you have to wait till asyncore.loop (default is 30s).
I was trying to work around it in two ways 1) Reduce time expiration for a low price or 2) When they will make next updates and generate enough time-value values, however When you mention 'selection law' in 'man 2 select_tut', it says, "You should always try to use selection () without time."
Is there a better way to do this? Maybe twisted? I want to try the extra thread. I will include a variable timeout example here:
#! / Usr / bin / python import time import socket import asyncore UPDATE_PERIOD in # seconds = 4.0 square channel (asyncore.dispatcher): def __init__ (self, sock, sck_map): asyncore.dispatcher .__ init __ (auto, sock = sock, Map = sck_map) should automatically update to self.last_update = 0.0 # self.send_buf = 'self.recv_buf =' def writeable (self): return LAN (self.send_buf) & gt; 0 def handle_write (self): nbytes = self.send (self.send_buf) self.send_buf = self.send_buf [nbytes:] def handle_read (self): print 'read' print 'recv:', self.recv (4096) Def handle_close (self): print 'closed' self.close () # variable timeout def added for update (self): if time.time ()> = Self.next_update (): self.send_buf + = 'Hello% f \ N'% (time.time ()) self.last_update = time.time () def next_update (auto): return self.last_update + UPDATE_PERIOD class server (Asyncore.dispatcher): def __init __ (auto, port, sck_map): asyncore.dispatcher .__ init __ (self, map = sck_map) self.port = port self.sck_map = sck_map self.create_socket (socket.AF_INET, socket .SOCK_STREAM) self.bind ("", port) self.listen (16) print "listen to the port", self .port def handlextip (self): (cone, eder) = self.accept () channel (sock = Conn, sck_map = self.sck_map) def def update (self) for the time: pass def next_update (auto): return any sck_map = {} server = all (9090, sck_map) while right: next_update = time.time () + 30.0 C sck_map.values (for): c.update () # & lt; - Write buffers n = c.next_update () #print 'n:', n If n is not none: next_update = min (next_update), N) _timeout = max (0.1, next_update - time.time ()) asyncore Loop (timeout = _timeout, count = 1, map = sck_map)
"select law "Does not apply to your case, because you have not only implemented activities of client-triggers (pure server), but also time-triggered activities - it's fine that the selection is out of time what the law really means. What should we say "If you specify an expiration, make sure you have to do something really useful when you come from time to time". The law is meant to save from busy waiting; Your code is not busy.
I can not set _timeout at maximum 0.1 and next update time, but for maximum 0.0 and next timeout. IOW, if the update has expired while updating the update, then you should immediately make that specific update.
Each channel has to ask every time whether it wants to update, instead, you can store all channels in one priority rows (sorted by next-update time), and its Afterwards run updates for only the initial channels (unless you are updated). You can use the Hoop module for that.
You can save some system calls by not asking each channel for the current time, but only once can choose the current time, and pass it.
Comments
Post a Comment