Author Topic: Python TCP server - CPU usage continually increasing over time  (Read 7542 times)

0 Members and 1 Guest are viewing this topic.

Online paulca

  • Super Contributor
  • ***
  • Posts: 4003
  • Country: gb
Re: Python TCP server - CPU usage continually increasing over time
« Reply #25 on: February 17, 2018, 09:34:08 am »
Python is weird, but is [ [ ] ] really a list of lists?

I would be more used to [][]

This kind of stuff, you are doing, is begging for a more C like language.  There is no ambiguity there with memory allocation and usually if you get something wrong you get a segfault.

Can you run your compiled Python in Valgrind?  Valgrind or KCacheGrind will show you memory allocation, deallocation, leaks and where they are originating.
"What could possibly go wrong?"
Current Open Projects:  STM32F411RE+ESP32+TFT for home IoT (NoT) projects.  Child's advent xmas countdown toy.  Digital audio routing board.
 

Offline DeltaTopic starter

  • Super Contributor
  • ***
  • Posts: 1221
  • Country: gb
Re: Python TCP server - CPU usage continually increasing over time
« Reply #26 on: February 18, 2018, 03:43:50 am »
I think your slice math might be wrong:

Code: [Select]
[register * 2:(register * 2)+3]
This doesn't look right to me.. again not entirely sure of your intentions. But I can tell you, this is not the reason why your code is running slower. I just ran this in interactive iPython:


Nope, you were correct; that IS the reason my code was running slower!

It needs to be
Code: [Select]
[register * 2:(register * 2)+4]You need to slice past the last item you want.  :palm:

Doing
Code: [Select]
test[0:3] = bytearray(struct.pack(">f", 123.456 ))
tries to put a FOUR byte float into the first THREE positions, well it does put it into the first four positions, but what was the old fourth byte gets shoved to the right and retained.

My code was working because I do these writes in sequence, so the rogue bytes got overwritten.

Oh well, I learned a lot about debugging, and my shitty code is now all running fine.

I promise my next script will be much neater!

 

Offline djnz

  • Regular Contributor
  • *
  • Posts: 179
  • Country: 00
Re: Python TCP server - CPU usage continually increasing over time
« Reply #27 on: February 18, 2018, 10:04:04 am »
One way to intuitively remember it is [a:b] has, in the simplest case,  length b-a. This has helped me avoid off-by-one errors in a number of situations.
 
The following users thanked this post: Delta


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf