1

I know using sockets may not be the best way to do this but I've got pretty some simple code developed where I send data from one Raspberry Pi to another using Python sockets (UDP) but I haven't been able to find an example on how to create an interrupt for when data is received. Basically I've got a process running and I don't want it to be held waiting for data to be received by s.recvfrom. I attached basic code below and I could use some help pointing me in the right direction.

import socket
import sys
import os

UDP_IP = '192.168.0.96'
UDP_PORT = 5008

data = "abcd"
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', UDP_PORT))

while(1)
    data, addr = s.recvfrom(1024)
user3723727
  • 142
  • 1
  • 4
  • 15

2 Answers2

0

I can't comment yet or I would.

I don't know exactly what you are using the data for, but you might be able to thread out the UDP connection and append the data to a list or database. Then it should be as simple as accessing that list/database when your script needs to process the data.

alisenby94
  • 64
  • 5
  • Thank you for the reply! I thought of threading it but I haven't done much in the way of threads. I was really hoping on getting an interrupt since then I wouldn't have to worry about checking the status and all I would have to do is just wait for an interrupt. One aspect of what I'm trying to do is send a command to the other Pi telling it to kill the process. – user3723727 Sep 23 '15 at 05:40
  • So if I understand you correctly, you want the other Pi to connect back and notify your script via UDP that it needs to be killed, then the script responds telling it that it can kill the process? So basically this Pi is monitoring the other in the event that the script is finished/fails/hangs. – alisenby94 Sep 23 '15 at 07:00
  • Basically just have a pi running a process and when it receives an "interrupt" (from the other Pi via a socket) saying there is data from the other Pi than it reads the command and kills the process (or do whatever the command says). – user3723727 Sep 23 '15 at 14:23
0

You can use select (https://docs.python.org/2/library/select.html) to find out which socket has io waiting and process it. This is quite efficient and you don't have to worry about interrupts.

sureshvv
  • 4,234
  • 1
  • 26
  • 32
  • Thank you for the suggestion. I've looked at the "select" and I'm having a hard time figuring out how it works. Do you know of an example (can't find one) that I can take a look at? – user3723727 Sep 23 '15 at 14:27
  • Here is the C version. See if you can use it in a similar fashion http://stackoverflow.com/questions/15592089/implementing-udp-sockets-with-select-in-c – sureshvv Sep 23 '15 at 17:55
  • I found an example but I have issues so I created a new post here: http://stackoverflow.com/questions/32753709/python-socket-using-select-to-check-for-data?noredirect=1#comment53347669_32753709 – user3723727 Sep 24 '15 at 05:22
  • @user3723727 Have answered u there – sureshvv Sep 24 '15 at 06:04