Muy buenas gente de Underc0de quería compartir con vosotros un módulo que hice para la comunicación entre hilos y aunque muchos sabéis que existe la librería estándar de "Queue" yo decidí crear una propia por que tenia algunos problemillas a la hora de comunicar los datos entre hilos.
threadComm.py
requiere: PyQT4
#By: HackLoper
#twiter: https://twitter.com/HackLoper
#canal-youtube: hackdeveloper
#facebook: hackloper
#site: http://hackloper.blogspot.com.es/2016/03/python-comunicacion-entre-hilos.html
from PyQt4 import QtCore
import time,sys
class Error(Exception):
def __init__(self,error):
self.error = error
def __str__(self):
return self.error
class Queue:
def __init__(self,max_data=1024,max_buffer=1024):
self.__buffer = []
self.__writing = False
self.__getting = False
self.__max_data = max_data
self.__max_buffer = max_buffer
def __del_data_buffer(self):
while self.len_buffer() > self.__max_buffer:
del self.__buffer[0]
def set_data(self,data):
if self.__writing == True:
return (False,'set_data','writing')
elif sys.getsizeof(data) > self.__max_data:
return (False,'set_data','size data not supported')
else:
if self.len_buffer() > self.__max_buffer:
self.__del_data_buffer()
self.__writing = True
self.__buffer.append(data)
self.__writing = False
return (True,'set_data')
def wait_data(self,num_loop=0,time_wait=1):
if num_loop == 0 and time_wait > 0:
return self.get_data(True)
elif num_loop > 0 and time_wait > 0:
num = 0
while num < num_loop:
if self.len_buffer() > 0:
return (True,'wait_data',self.get_data(True))
time.sleep(time_wait)
return(False,'wait_data',[])
else:
if num_loop < 0:
raise Error('Error num_loop with signed')
elif time_wait < 0:
raise Error('Error time_wait with signed')
def get_data(self,call=False):
if self.__getting == True:
return (False,'get_data','getting')
self.__getting = True
temp_buffer = self.__buffer
self.__buffer = []
self.__getting = False
if call == True:
return temp_buffer
return (True,'get_data',temp_buffer)
def len_buffer(self):
if len(self.__buffer) > 0:
return sys.getsizeof(self.__buffer)
else:
return 0
class Thread(QtCore.QThread):
__error_queue = []
def __init__(self,queues=[]):
QtCore.QThread.__init__(self)
for queue in queues:
if queues[queue].__class__.__name__ == 'Queue':
setattr(self,'_{0}'.format(queue),queues[queue])
else:
self.__error_queue.append("Not supported object because it is not of type Queue, object {0} class {1}".format(queue,\
queues[queue].__class__.__name__))
if len(self.__error_queue) > 0:
raise Error(str(self.__error_queue))
Un ejemplo:
from threadComm import Thread,Queue
import time
class th(Thread):
def __init__(self,**queues):
Thread.__init__(self,queues)
def run(self):
for i in range(15):
self._com.set_data(i+1)
time.sleep(1)
class th2(Thread):
def __init__(self,**queues):
Thread.__init__(self,queues)
def run(self):
n = 0
while n != 15:
print "N vale: "+str(n)
data = self._q.wait_data(3,1)
if data[0] == True:
n = data[2]
q = Queue()
t = th(com=q)
t2 = th2(q=q)
t.start()
t2.start()
for i in range(17):
print "hilo princial: "+str(i)
time.sleep(1)
Resultado:
hilo princial: 0
N vale: 0
N vale: [1]
hilo princial: 1
N vale: [2]
hilo princial: 2
N vale: [3]
hilo princial: 3
N vale: [4]
hilo princial: 4
N vale: [5]
hilo princial: 5
N vale: [6]
hilo princial: 6
N vale: [7]
hilo princial: 7
N vale: [8]
hilo princial: 8
N vale: [9]
hilo princial: 9
N vale: [10]
hilo princial: 10
N vale: [11]
hilo princial: 11
N vale: [12]
hilo princial: 12
N vale: [13]
hilo princial: 13
N vale: [14]
hilo princial: 14
N vale: [15]
hilo princial: 15
hilo princial: 16
Espero que os sirva en algún script o programita :)