# Sending data from python to processing

**URL:** <https://discourse.processing.org/t/sending-data-from-python-to-processing/25771>\
**Category:** Libraries\
**Created:** [November 27, 2020, 6:09pm UTC](https://discourse.processing.org/t/sending-data-from-python-to-processing/25771 "2020-11-27T18:09:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![arcadeperfect](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arcadeperfect](https://discourse.processing.org/u/arcadeperfect)\
**Post date:** [November 27, 2020, 6:09pm UTC](https://discourse.processing.org/t/sending-data-from-python-to-processing/25771/1 "2020-11-27T18:09:02Z")

</div>

Hi  
I’m trying to transmit sensor data from a python script to a processing sketch via sockets.

On the python side I have:

```auto
import socket
import random

HOST = '' # Symbolic name meaning all available interfaces
PORT = 12000 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

while True:
    #s.send("test\n".encode());
    data = random.random() #simulated data
    data = str(data).encode()
    s.send(data)

```

and in Processing:

```auto
import processing.net.*;

Server s;

void setup() {
  s = new Server(this, 12000);
}

void draw() {
  
  println("new loop");
  
  // check if a client sent something
  Client c = s.available();
  if (c != null) {
    String input = c.readString();
    println(input);
  }
  delay(1000);
}

```

The random numbers print just fine in the Processing sketch, but while the Python script is running, the if statement in Processing never exits and so I get an endless stream of data, and the draw loop never repeats.

How can I have the Processing sketch sample exactly one value from the python script per draw loop?

Thanks!

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 27, 2020, 7:26pm UTC](https://discourse.processing.org/t/sending-data-from-python-to-processing/25771/2 "2020-11-27T19:26:35Z")

</div>

Hi,

I think your issue comes from the fact that in your Python script, you are making an endless loop in the `while` and the rate of execution is really high compared to the delay you put inside the `draw()` function in Processing.

So in one second (`1000` ms), your Python script is filling the socket buffer with random values but when you read them in Processing, it’s reading thousands of values.

I have the following solution :

- In your Python file, change this line to send a new line each time :

- In your Processing code, use the [`readStringUntil()`](https://processing.org/reference/libraries/net/Client_readStringUntil_.html) function to read from the buffer until it found that newline (which is converted to a byte) :

Now it should read only one random value. I don’t know if it’s the best way to do this but it works!

Hope it helps 😉

---

<div class="post-metadata">

**Author:** ![arcadeperfect](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arcadeperfect](https://discourse.processing.org/u/arcadeperfect)\
**Post date:** [November 27, 2020, 7:43pm UTC](https://discourse.processing.org/t/sending-data-from-python-to-processing/25771/3 "2020-11-27T19:43:27Z")

</div>

Indeed it does help!

Thanks!

---

<div class="post-metadata">

**Author:** ![arcadeperfect](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arcadeperfect](https://discourse.processing.org/u/arcadeperfect)\
**Post date:** [November 27, 2020, 7:48pm UTC](https://discourse.processing.org/t/sending-data-from-python-to-processing/25771/4 "2020-11-27T19:48:54Z")

</div>

That does work, but now I’m wondering if ideally would the processing sketch poll the python script when it wants an update rather than having python bombard it with data? Would I need to switch around the server / client for that?

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 27, 2020, 7:51pm UTC](https://discourse.processing.org/t/sending-data-from-python-to-processing/25771/5 "2020-11-27T19:51:58Z")

</div>

Yeah you are right, is this your entire code? Because otherwise this example is not really optimized and it would make no sense to just send a random value to Processing when you can do the same in Java.

So if not what do you ideally want to send to Processing?

---

<div class="post-metadata">

**Author:** ![arcadeperfect](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arcadeperfect](https://discourse.processing.org/u/arcadeperfect)\
**Post date:** [November 27, 2020, 7:57pm UTC](https://discourse.processing.org/t/sending-data-from-python-to-processing/25771/6 "2020-11-27T19:57:49Z")

</div>

This is just my attempt to work out the protocol to slot it into a much larger program. The reason to combine python and processing, is that I have a gyro sensor that comes with a python lib (for raspberry pi), and I want it to control the gravity of a [simulation](https://vimeo.com/483832448) I have that runs in processing. If I could find a lib to interface with the sensor directly from Processing I would, but haven’t found one.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 27, 2020, 8:17pm UTC](https://discourse.processing.org/t/sending-data-from-python-to-processing/25771/7 "2020-11-27T20:17:35Z")

</div>

Nice video! 🙂

So yes ideally it’s not a good idea to spam your computer with sockets so fast.

I thought about something : in python in your while loop, only send data if the buffer is empty (if reading it return no bytes) and in your Processing sketch, if the buffer is not empty, read it. You can do this at every frame instead of waiting a delay I think.

This is going to synchronize both the Processing and Python sketch. I’m not an expert of network and sockets but it makes me think of synchronization mechanism with [semaphores](https://en.wikipedia.org/wiki/Semaphore_(programming)).

Anyway you can look at this thread to dig into this :

> <https://stackoverflow.com/questions/55160527/why-does-server-and-client-gets-out-of-sync-python-sockets>

---

<div class="post-metadata">

**Author:** ![arcadeperfect](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arcadeperfect](https://discourse.processing.org/u/arcadeperfect)\
**Post date:** [November 27, 2020, 8:41pm UTC](https://discourse.processing.org/t/sending-data-from-python-to-processing/25771/8 "2020-11-27T20:41:35Z")

</div>

Thanks, will look into it!
