I'm having continuity problems in my code after a function call, related to receiving data via socket with the following code:
def le_dados(obj):
lista_rcv = []
while True:
data = obj.recv(2)
if data:
lista_rcv.append(data)
if len(lista_rcv) == 4:
lista_rcv.append(obj.getpeername())
global status_central
status_central = lista_rcv
lista_rcv = []
sleep(0.25)
This function is called inside another function like this:
process = multiprocessing.Process(target=le_dados(s))
process.daemon = True
process.start()
What I can not visualize, maybe because of lack of experience is because the code stops on the line:
data = obj.recv(2)
By not letting the code move forward in the process
call,
is stopped here:
process = multiprocessing.Process(target=le_dados(s))
So I do not let my Gui come in after that.
Just complementing, it follows the rest of the code, it's used to connect to devices where I'll send and receive commands, with one working, but when I try with several I'm not getting it.
import socket
import os
import Gcomn
from time import sleep
import multiprocessing
PORT = 7557
status_central = []
on_off_disp = []
def conecta_dispositivos():
sql = 'select nome_disp, Ip_disp from dispositivos'
dados_dis = Gcomn.le_dados(sql)
global on_off_disp
on_off_disp = []
for i in range (len(dados_dis)):
try:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#s.settimeout(5)
s.connect((dados_dis[i][1] ,7557))
if s:
print("Conectado a: ", s.getpeername())
sleep (1.5)
sendData()
on_off_disp.append(1)
print('vou chamar o process')
process = multiprocessing.Process(target=recebe_dados(s))
process.daemon = True
process.start()
print('depois do process')
except socket.error:
print ('Erro ao conectar em: ', dados_dis[i][0])
on_off_disp.append(0)
def recebe_dados(obj):
lista_rcv = []
obj.setblocking(False)
while True:
data = obj.recv(2)
if data:
lista_rcv.append(data)
if len(lista_rcv) == 4:
lista_rcv.append(obj.getpeername())
global status_central
status_central = lista_rcv
lista_rcv = []
else:
print('não recebi nada')
sleep(0.25)
def sendData(comando = 'A00000'):
s.send(comando.encode('utf-8'))
conecta_dispositivos()