3
I am experiencing continuity issues 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 within another function like this:
process = multiprocessing.Process(target=le_dados(s))
process.daemon = True
process.start()
What I’m failing to visualize, perhaps from lack of experience, is why the code stops on the line:
data = obj.recv(2)
Not letting the code go ahead on the call of process
,
stands here:
process = multiprocessing.Process(target=le_dados(s))
not leaving so I amaze my Gui that comes after that.
Only complementing, follow the rest of the code, it is used to connect to devices where I will send and receive commands, with only one worked, but when I try with several I am not getting.
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()
Probably the call
recv
is blocking the execution until it receives some data through the socket. You can set the connection not to lock the program as soon as you create the socketobj
withobj.setblocking(False)
, however, you will need to address the case where you called therecv
and received no data– Gomiero
Thanks for the answer, by doing this, the program jumps and executes the rest, however, the block that is called in the process that is le_dados(), does not work.
– Rafa710
If you change the
socket
not to block, you will also need to change your algorithm. With the code that is in the question ("locking"), you tried to connect tosocket
from another program (eg telnet) and send some data? It has not unlocked?– Gomiero
I’m trying to connect with some devices that work in server mode, I made a code that works with one and sends and receives commands normally but when I try to connect with several appears the problem. I made a loop for and got a list from the bank with the registered devices and try to connect with each one. Maybe that’s the problem. don’t know how to put all the code in the same question. just editing?
– Rafa710
Yes. Just edit the question and add all that information and code! :)
– Gomiero