def run(self): #print(self.run, 'enter') while True: #print(self.run, 'spin') self.userv.wi_available.acquire() while len(self.userv.wi) == 0: self.userv.wi_available.wait() wi = self.userv.wi.pop(0) if wi == None: # Shutdown request, relay it further self.userv.wi.append(None) self.userv.wi_available.notify() self.userv.wi_available.release() if wi == None: break command, result_callback, callback_parameters = wi try: data, rtpc_delay = self.send_raw(command) if len(data) == 0: data, rtpc_delay = None, None except Exception as e: dump_exception('Rtp_proxy_client_stream: unhandled exception I/O RTPproxy') data, rtpc_delay = None, None if result_callback != None: ED2.callFromThread(self.dispatch, result_callback, data, callback_parameters) if rtpc_delay != None: ED2.callFromThread(self.userv.register_delay, rtpc_delay) self.userv = None
def run(self): #print(self.run, 'enter') while True: #print(self.run, 'cycle') self.w_available.acquire() while self.wbuffer != None and len(self.wbuffer) == 0 and not self.close_pendind: self.w_available.wait() if self.wbuffer == None: self.w_available.release() break wbuffer = self.wbuffer if not self.close_pendind: self.wbuffer = bytes() else: self.wbuffer = None self.w_available.release() while True: res = self.clientsock.send(wbuffer) if res == len(wbuffer): break if res > 0: wbuffer = wbuffer[res:] if self.close_pendind: ED2.callFromThread(self.clim.shutdown) self.clim = None
def run(self): #print(self.run, 'enter') while True: #print(self.run, 'cycle') self.w_available.acquire() while self.wbuffer != None and len( self.wbuffer) == 0 and not self.close_pendind: self.w_available.wait() if self.wbuffer == None: self.w_available.release() break wbuffer = self.wbuffer if not self.close_pendind: self.wbuffer = bytes() else: self.wbuffer = None self.w_available.release() while True: res = self.clientsock.send(wbuffer) if res == len(wbuffer): break if res > 0: wbuffer = wbuffer[res:] if self.close_pendind: ED2.callFromThread(self.clim.shutdown) self.clim = None
def signal_handler(self, signum, *frame): ED2.callFromThread(self.dispatch) if self.previous_handler not in (SIG_IGN, SIG_DFL): try: self.previous_handler(signum, *frame) except: dump_exception('Signal: unhandled exception in signal chain')
def run(self): maxemptydata = 100 while True: try: data, address = self.userv.skt.recvfrom(8192) if not data and address == None: # Ugly hack to detect socket being closed under us on Linux. # The problem is that even call on non-closed socket can # sometimes return empty data buffer, making AsyncReceiver # to exit prematurely. maxemptydata -= 1 if maxemptydata == 0: break continue else: maxemptydata = 100 rtime = MonoTime() except Exception as why: if isinstance(why, socket.error) and why.errno in (ECONNRESET, ENOTCONN, ESHUTDOWN): break if isinstance(why, socket.error) and why.errno in (EINTR,): continue else: print(datetime.now(), 'Udp_server: unhandled exception when receiving incoming data') print('-' * 70) traceback.print_exc(file = sys.stdout) print('-' * 70) sys.stdout.flush() sleep(1) continue if self.userv.uopts.family == socket.AF_INET6: address = ('[%s]' % address[0], address[1]) ED2.callFromThread(self.userv.handle_read, data, address, rtime) self.userv = None
def run(self): #print(self.run, 'enter') while True: #print(self.run, 'spin') self.userv.wi_available.acquire() while len(self.userv.wi) == 0: self.userv.wi_available.wait() wi = self.userv.wi.pop(0) if wi == None: # Shutdown request, relay it further self.userv.wi.append(None) self.userv.wi_available.notify() self.userv.wi_available.release() if wi == None: break command, result_callback, callback_parameters = wi try: data, rtpc_delay = self.send_raw(command) if len(data) == 0: data, rtpc_delay = None, None except Exception as e: dump_exception( 'Rtp_proxy_client_stream: unhandled exception I/O RTPproxy' ) data, rtpc_delay = None, None if result_callback != None: ED2.callFromThread(self.dispatch, result_callback, data, callback_parameters) if rtpc_delay != None: ED2.callFromThread(self.userv.register_delay, rtpc_delay) self.userv = None
def _stop_play(self, rtpps, result_callback = None, index = 0): if not self.session_exists: ED2.callFromThread(rtpps.command_result, None, result_callback) return from_tag, to_tag = self.gettags(rtpps) command = 'S %s %s %s' % ('%s-%d' % (rtpps.call_id, index), from_tag, to_tag) rtpps.rtp_proxy_client.send_command(command, rtpps.command_result, result_callback)
def run(self): #print(self.run, 'enter') while True: #print(self.run, 'cycle') pollret = dict(self.pollobj.poll()).get(self.fileno, 0) if pollret & POLLNVAL != 0: break if pollret & POLLIN == 0: continue try: clientsock, addr = self.clicm.serversock.accept() except Exception as why: if isinstance(why, socket.error): if why.errno == ECONNABORTED: continue elif why.errno == EBADF: break else: raise dump_exception( 'CLIConnectionManager: unhandled exception when accepting incoming connection' ) break #print(self.run, 'handle_accept') ED2.callFromThread(self.clicm.handle_accept, clientsock, addr) self.clicm = None
def run(self): maxemptydata = 100 while True: try: data, address = self.userv.skt.recvfrom(8192) if not data and address == None: # Ugly hack to detect socket being closed under us on Linux. # The problem is that even call on non-closed socket can # sometimes return empty data buffer, making AsyncReceiver # to exit prematurely. maxemptydata -= 1 if maxemptydata == 0: break continue else: maxemptydata = 100 rtime = MonoTime() except Exception as why: if isinstance(why, socket.error) and why[0] in (ECONNRESET, ENOTCONN, ESHUTDOWN): break if isinstance(why, socket.error) and why[0] in (EINTR,): continue else: print(datetime.now(), 'Udp_server: unhandled exception when receiving incoming data') print('-' * 70) traceback.print_exc(file = sys.stdout) print('-' * 70) sys.stdout.flush() sleep(1) continue if self.userv.uopts.family == socket.AF_INET6: address = ('[%s]' % address[0], address[1]) ED2.callFromThread(self.userv.handle_read, data, address, rtime) self.userv = None
def _play(self, rtpps, prompt_name, times = 1, result_callback = None, index = 0): if not self.session_exists: ED2.callFromThread(rtpps.command_result, None, result_callback) return otherside = self.getother(rtpps) if not otherside.session_exists: otherside.update(rtpps, '0.0.0.0', 0, self.__play, '', index, 'IP4', prompt_name, times, result_callback, index) return self.__play(None, rtpps, prompt_name, times, result_callback, index)
def requestCallback(self, resp, *args, **kwargs): query = parse_qs(resp.url) param = { query['state'][0]: resp.text, "url": resp.url, "diff": round(time.time() * 1000) - self.time_invite } print('threading.currentThread =>', threading.currentThread().ident) ED2.callFromThread(self.createUa0, param)
def run(self): need_close_fds = True if platform == 'win32': need_close_fds = False pipe = Popen(self.command, shell = False, stdin = PIPE, \ stdout = PIPE, stderr = PIPE, close_fds = need_close_fds) while True: self.master.work_available.acquire() while len(self.master.work) == 0: self.master.work_available.wait() wi = self.master.work.pop(0) if wi == None: # Shutdown request, relay it further self.master.work.append(None) self.master.work_available.notify() self.master.work_available.release() if wi == None: break if wi.is_cancelled(): wi.data = None wi.result_callback = None wi.callback_parameters = None continue batch = [x + b'\n' for x in wi.data] batch.append(b'\n') pipe.stdin.writelines(batch) pipe.stdin.flush() result = [] while True: try: line = pipe.stdout.readline().strip() except IOError as e: # Catch EINTR if e.args[0] != EINTR: raise e continue if len(line) == 0: break result.append(line) ED2.callFromThread(self.master.process_result, wi.result_callback, tuple(result), *wi.callback_parameters) wi.data = None wi.result_callback = None wi.callback_parameters = None
def run(self): #print(self.run, 'enter') while True: #print(self.run, 'cycle') pollret = dict(self.pollobj.poll()).get(self.fileno, 0) if pollret & POLLNVAL != 0: break if pollret & POLLIN == 0: continue try: clientsock, addr = self.clicm.serversock.accept() except Exception as why: if isinstance(why, socket.error): if why.errno == ECONNABORTED: continue elif why.errno == EBADF: break else: raise dump_exception('CLIConnectionManager: unhandled exception when accepting incoming connection') break #print(self.run, 'handle_accept') ED2.callFromThread(self.clicm.handle_accept, clientsock, addr) self.clicm = None
def run(self): rbuffer = '' while True: data = self.clientsock.recv(1024) if len(data) == 0: ED2.callFromThread(self.clim.shutdown) break try: rbuffer += data.decode('ascii') except UnicodeDecodeError: ED2.callFromThread(self.clim.shutdown) break while rbuffer.find('\n') != -1: cmd, rbuffer = rbuffer.split('\n', 1) cmd = cmd.strip() if len(cmd) == 0: continue ED2.callFromThread(self.clim.handle_cmd, cmd) self.clim = None
def __del__(self): if self.my_ident != get_ident(): #print 'Rtp_proxy_session.__del__() from wrong thread, re-routing' ED2.callFromThread(self.delete) else: self.delete()
def run(self): rfile = None session_timeout = self.default_timeout ctime = None count = 0 port = Serial(self.sdev, baudrate = self.brate, timeout=0.1) while True: try: data = port.read(256) except Exception as e: self.lfile.write('Session exception: %s\n' % str(e)) self.lfile.flush() if rfile != None: #rfile.flush() rfile.close() rfile = None sleep(1) port = Serial(self.sdev, baudrate = self.brate, timeout=0.1) continue atime = time() if rfile != None and atime - ctime > session_timeout: self.lfile.write('Session timeout: %f\n' % (atime - ctime)) self.lfile.flush() #rfile.flush() rfile.close() rfile = None if self.send_cb != None: ED2.callFromThread(self.send_cb) self.enqueue_cb_lock.acquire() self.enqueue_cb = None self.enqueue_cb_lock.release() self.prequeue = [] if len(data) == 0: continue previous_ctime = ctime ctime = atime if rfile == None: fname = '/tmp/%s.csv' % int(ctime) rfile = open(fname, 'w') session_timeout = self.default_timeout previous_ctime = None count = 0 self.lfile.write('Starting recording %s\n' % fname) self.lfile.flush() if self.sstart_cb != None: ED2.callFromThread(self._sstart_cb) if previous_ctime != None and session_timeout > (ctime - previous_ctime) * 2 and count > 2: session_timeout = (ctime - previous_ctime) * 2 self.lfile.write(' Updating session timeout to %f sec\n' % session_timeout) self.lfile.flush() parts = [x.strip() for x in data.decode('ascii').split(' ', 3)] try: volts = float(parts[1][:-1]) amps = float(parts[2][:-1]) except: count += 1 continue pload = '%d,%f,%f' % (count, volts, amps) rfile.write('%s\n' % pload) pload = pload.encode() self.enqueue_cb_lock.acquire() if self.enqueue_cb != None: self.enqueue_cb(pload) else: self.prequeue.append(pload) self.enqueue_cb_lock.release() #rfile.flush() count += 1