Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>def handle_connect_event(self): self.is_connected = True </code></pre> <p>Adding that to your dispatcher will give you a way to check if the socket is connected or not, thanks to some stack trace (<code>python -m trace -t script.py</code>) in Python I managed to figure out that the <code>asyncore</code> class automaticly created that function for whatever reason, and it was called continiously as long as the socket was connected or in a connected state.</p> <p>After that, i also replaced the threaded asyncore.loop() and replaced it with a "static" placement locking your main thread, one of these two combinations (or both) solved the issue for now.. the logic isn't the same as in my problem which i don't like but i assume that i'll be needing to create my own dispach_event just like if i were to do a OpenGL GUI class where i would call dispatch_event() manually every loop some how in the thread to "keep things alive".. it's just a thought..</p> <p>Anyway, here's a working example:</p> <pre><code>#!/usr/bin/python # -*- coding: iso-8859-15 -*- import asyncore, socket from threading import * from time import sleep from os import _exit from logger import * from config import * def _map(): return {} def _array(): return [] class logDispatcher(Thread, asyncore.dispatcher): def __init__(self, config=None): self.inbuffer = '' self.buffer = '' self.lockedbuffer = False self.is_writable = False self.is_connected = False self.exit = False self.initated = False asyncore.dispatcher.__init__(self) Thread.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) try: self.connect((server, server_port)) except: log('Could not connect to ' + server, 'LOG_SOCK') return None self.start() def handle_connect_event(self): self.is_connected = True def handle_connect(self): self.is_connected = True log('Connected to ' + str(server), 'LOG_SOCK') def handle_close(self): self.is_connected = False self.close() def handle_read(self): data = self.recv(8192) while self.lockedbuffer: sleep(0.01) self.inbuffer += data def handle_write(self): while self.is_writable: sent = self.send(self.buffer) sleep(1) self.buffer = self.buffer[sent:] if len(self.buffer) &lt;= 0: self.is_writable = False sleep(0.01) def _send(self, what): self.buffer += what + '\r\n' self.is_writable = True def run(self): sleep(1) log('Log engine initating (hooking on to main)', 'LOG_CORE') main = None for t in enumerate(): if t.getName() == 'MainThread': main = t log('Log engine attached to main', 'LOG_CORE') while (main and main.isAlive()) and (self.connected or self.is_connected): print 'WHAM', self.connected, self.is_connected sleep(1) while 1: logDisp = logDispatcher() asyncore.loop(0.1) log('Logserver disconnected, trying to reconnect!', 'CORE') sleep(10) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload