0
$\begingroup$

the question is related to networking frameworks; callback-based approach requires you to call a callback function every time you receive a new data packet; this is not a good approach for high-load and high-throughput network servers; is it faster to have a 'while-true' loop running in a separate process or a thread, checking for new packets continuously, then passing received data back to the original process, particularly in Python?

$\endgroup$

1 Answer 1

2
$\begingroup$

callback-based approach requires you to call a callback function every time you receive a new data packet; this is not a good approach for high-load and high-throughput network servers

Who claims that? Node.js, for example, which was specifically designed to avoid overhead in handing high load, uses callback functions exactly for this reason.

is it faster to have a 'while-true' loop running in a separate process or a thread, checking for new packets continuously, then passing received data back to the original process

No. A function call will always be faster. Calling a function normally just means pushing a stack frame and setting the instruction counter. Although you don't specify what kind of packages you want to send, IP and UDP or TCP will introduce significantly bigger overhead. Note that sending such a package will normally already involve at least one function call.

You should also consider that busy polling (while true loop) will waste energy and potentially cause lots of useless context switches, if you don't have enough cores for all processes you're running.

$\endgroup$
4
  • $\begingroup$ what exactly happens if packets are coming faster than a single cycle of a 'while true' loop takes time to complete? will packets be dropped, cached or simply the whole application begins to clog and freeze, potentially unfreezing when no more packets are left in the buffer? $\endgroup$
    – ivan866
    Commented Jul 27, 2021 at 14:46
  • 1
    $\begingroup$ The operating system will queue packages for you until you retrieve them. If packages arrive too fast, they'll be dropped eventually. The exact numbers depend on the OS, configuration (and maybe hardware). $\endgroup$
    – idmean
    Commented Jul 27, 2021 at 14:47
  • $\begingroup$ what if i need to process a udp packet almost in real-time, say, in less than 0.8 ms? how do i approach this task? $\endgroup$
    – ivan866
    Commented Jul 27, 2021 at 15:06
  • $\begingroup$ This cannot be answered like this. What do this package contain? What does it mean for one of these packages to be processed? 0.8ms measured from when to when? Etc. In general: 1. Pick a systems programming language, Python will give you a considerable performance hit. 2. Look for and implement or use implementations of efficient algorithms. 3. Profile and optimize. $\endgroup$
    – idmean
    Commented Jul 27, 2021 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.