0

I have a server that receives audio stream data from a client and then sends it to all other connected clients (via sockets). Since the data stream makes the code stuck on a loop , I decided that I should create processes for each stream (client essentially). I noticed that even with one connected client , the process makes the audio lag. Why is that ? That also happens if I try to use threads.

Code:

p_r = Process(target=playback_f,
              args=(pass_record, soc, client, port_a, data, c)).start()

Where soc is the socket connection of the client in question, data is the initial data and c is the result of:

c, addr1 = s.accept()

The process calls:

def playback_f(pass_record, soc, client, port_a, data, c):
    try:
        stop_recording = False
        try :
            soc.sendto('playback'.encode('utf-8'),(client,port_a))
        except Exception as e:
            print (e)

        while stop_recording == False:
            if data !=  b'stop_rec':
                try:
                    data = c.recv(1024)
                    soc.sendto(data,(client,port_a))
                except Exception as e:
                    print (e)
            else:
                stop_recording = True

        print('ended')

    except Exception as e:
        print(e)
martineau
  • 119,623
  • 25
  • 170
  • 301
Alex Walker
  • 91
  • 2
  • 9
  • 1
    Without any sample code it's very hard to provide help. Can you look at [these guidelines](https://stackoverflow.com/help/mcve) for improving your post? – Ron Norris Jul 08 '17 at 19:55
  • added some code – Alex Walker Jul 08 '17 at 19:56
  • Normally, I'd say "yes" because of the additional overhead, but since you say it doesn't happen with threads, it make me think there's another cause. – martineau Jul 08 '17 at 20:07
  • No , it basically happens both with threads and processes. So what approach to I take to avoid the lag ? – Alex Walker Jul 08 '17 at 20:09
  • The usual workaround is to buffer the data, at least until enough is obtained to be able to be used for some if not most of the rest of the processing (assuming more keeps coming in while this is happening). There will still be a lag, but most or all of it will occur at the very beginning. – martineau Jul 08 '17 at 20:12
  • The connection isn't the OP's issue so much as total throughput (TPT). Not sure whether it's the client or the server at fault for delays/lags in the stream. It's not likely the socket implementation at the Python level, but more probably the handling of the stream on either the client or server side. I don't see any obvious delays in the provided code. – Ron Norris Jul 08 '17 at 20:12

2 Answers2

0

It's really hard to debug without a code or any sorts of specifications. However, I do have a possible idea why this lag might be occurring. When you use multiprocessing, you are creating multiple processes with each process trying to connect to the same external socket at the same time. Multiple simultaneous connections can get laggy on a bad internet. Moreover, if you don't use locks, you'll get an erroneous output and if you use them, it'll lead to further possible lag. Hence it is better to avoid multiprocessing in this case. As for threading, this might occur due to a built-in locking system called GIL.

Harshith Thota
  • 856
  • 8
  • 20
0

Ok , I solved it. Basically , the lag was the result of my main code running in loops trying to receive a message , while at the same time , my process was doing the same thing. The way to solve this way to change the code in a way that eliminates this server overload.

Alex Walker
  • 91
  • 2
  • 9