Tobse
Tobse

Reputation: 37

Python process blocking socket [Errno 98]

I'm running two raspis connected to each other. They both run a program on startup (which starts fine). However, I keep getting a error: [Errno 98] Address already in use error in the background, and the communication between both Pis doesn't even start.

I went for tracing down the python processes running via ps -fA ¦ grep python and tadaaa, I can see, that another process starts trying to use the same address. The output of ps looks like:

root      923  917  0  12:25 pts/1   00:00:00 sudo python /home/pi/Documents/3_multithread.py
root      927  923  10 12:25 pts/1   00:00:00 python /home/pi/Documents/3_multithread.py
pi        932  881  0  12:25 pts/0   00:00:00 grep --color=auto python

From what I understand is, that it looks like the same process (3_multithread.py) is called twice (once as su, once not as su).

EDIT#2: I start the program via the .bashrc (sudo python /home/pi/Documents/3_multithread.py) and a lxsession. Might this be the reason for blocking the address?

The errorlog looks like that:

File "/home/pi/Documents/3_multithread.py", line 276, in set_server bind((HOST, PORT))
File "/usr/lib/python2.7/socket.py", line 228, in meth return getattr(self._sock, name)(*args)
error: [Errno 98] Address already in use

I just also checked the netstat -lptn. I get the following output:

tcp        0     0 0.0.0.0:22           0.0.0.0.*          LISTEN     -

EDIT#123: I traced the error further down. I imported and printed:

import os
os.getpid()

To get the process PID. Now what happens is: On bootup my script returned the PID 754, while the ps -fA ¦ grep python only stats two python processes with the PIDs 535 and 539. Does .bashrc and lxterminal start the process twice?

This is how i configure my socket inside the python script

def set_server():
    global conn_global
    global socket_global
    global conn_established
    HOST = ''
    PORT = 22
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr = s.accept()
    socket_global = s
    conn_global = conn
    conn_established = 1

I'm using global variables to send commands to send the commands to the other Pi, which is working absolutely perfect (once the connection is established).

Thank you guys very much in advance! With best regards, Tobi

Upvotes: 0

Views: 137

Answers (1)

Tobse
Tobse

Reputation: 37

So I was FINALLY able to solve that issue.

As Hannu suggested first, the problem was the way I booted the system. First of all, what did I want to do?

I need to boot both Raspis into the X environment (Graphical user interface), since both of my Pis are running a Tkinter GUI. That's why I went for the lxsession based autostart solution. Using the .bashrc file to start my python code failed, since it looked like the .bashrc is called twice on boot. So what solved my issue was, to use a shell script to start the python code on bootup from a lxterminal.

In order to do this:

  1. Create a shell script in your home directory (the tilde ~ directory)

    pi@raspberrypi:~ $ nano start.sh

and add the hashbang as well as the command to start your python script

#!/bin/sh
sudo python ~/Your/file/here.py
  1. Edit the user LX autostart file like:

    sudo nano ~/.config/lxsession/LXDE-pi/autostart

and add the call for the lxterminal including your shell script like

@lxterminal -e ./start.sh

Finally, for my application I had to grant access rights to everyone

chmod 775 start.sh

and use global file paths for all images, like

/home/Documents/Your/files/here.png

Save everything and reboot your pi. Now Everything should boot up perfectly. Thank you all for helping me, especially Hannu!

Upvotes: 1

Related Questions