0

I've made a socket server to listen for data to play a sound for a length of time. All is working fine, except I see that cpu runs constantly at 75%, and after some time I get an overheat warning.

I've tracked the problem down to the lines 'conn, addr = s.accept()' and 'data = conn.recv(bufferSize)'. The python program waits at these lines until connecting or receiving data respectively. Originally I thought it might have been the while loops, so I've placed and removed 'time.sleep()'s in various places with no luck. In the most current version posted here I've tried to give s.accept() a timeout and do some sleeping with a try/except but it still runs at 75%. I would expect the high cpu during the 1.5 seconds before timeout, followed by lower cpu for the 0.5 sleep but that is not the case.

Can anyone help me figure out what's up?

import socket
import pygame
import time

# Socket stuff
ip = '192.168.10.181'
port = 5005
bufferSize = 20

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((ip, port))
s.settimeout(1.5)
s.listen(1)

# Sound stuff
pygame.mixer.init()
pygame.mixer.music.load("vibration.wav")
pygame.mixer.music.play()
pygame.mixer.music.pause()

while 1:
        while 1:
                try:
                        conn, addr = s.accept()
                        print ('Connection address: ', addr)
                        break
                except:
                        print 'stuff'
                        time.sleep(0.5)

        time.sleep(1)
        data = conn.recv(bufferSize)
        if not data:
                break
        print ('received data: ', float(data))
        pygame.mixer.music.unpause()
        time.sleep(float(data))
        pygame.mixer.music.pause()
        conn.send(data)
        conn.close()
  • 1
    Comment out all the lines containing pygame. Does it still take 75% of the CPU? – joan Jul 22 '18 at 8:06
  • I've commented out all pygame lines and it still runs at 75% unfortunately. – Hollowzone Jul 22 '18 at 22:40
  • Strange. It does not when I run it on my laptop. Perhaps I'll try a Pi later. – joan Jul 23 '18 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.