Grasshoper freeze when use while function

Hello, i tried to use UDP listener with Ghpython but always Grasshopper freezes.
that happened always when i use while function, what is the problem ?

import socket

UDP_ip = "192.168.1.0"
UDP_port = 50025

serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

serverSock.bind(((UDP_ip,UDP_port)))

while True:
    data, addr = serverSock.recvfrom(1024)
    print "Message: ", data

Hi @seghierkhaled,

There’s no exit strategy for your while loop, which effectively means that it loops forever, which is called an infinite loop, and should be prevented at all costs! If you want to run it for some time (but not forever), you could do something like this:

count = 0

while True:
    if (count > 1000):
        break

    data, addr = serverSock.recvfrom(1024)
    print "Message: ", data
    count += 1

This loops 1001 times and then breaks out of the loop. You could even substitute the while with a for loop in this case.

1 Like

Thank you i will try that , i hope it work smoothly as udp receiver

while True == nightmare.

I guess what you want to do is to keep listening data from sent from somewhere. So just a 1000 times loop does not do the job.
I think what you need is a Task.


But a Task is extremely difficult to debug from within Grasshopper.
1 Like

Sadly didn’t work, always i got error

Runtime error (Exception): error

Traceback:
  line 228, in meth, "C:\Program Files\Rhino 6\Plug-ins\IronPython\Lib\socket.py"
  line 6, in script

Thanks , i just want to try if there is a smooth and simple solution
there are components exist to receive and send data

This seems to be another error! My recipe was a cure for Rhin/Grasshopper freezing.
I’m by no means an expert when it comes to UDP processes. :wink:

I will try your solution with animation, actually i stay with the exist UDP component: Ghowl or the one from Furobot