performance of script to write very long lines of random chars

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Apr 11 06:50:31 EDT 2013


On Thu, 11 Apr 2013 10:47:43 +0100, Oscar Benjamin wrote:

> On 11 April 2013 08:47, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
> 
>> One thing to be aware of: urandom may run out of entropy, and then it
>> will slow down a lot. If you don't care about cryptographic randomness,
>> you could use this instead:
> 
> Reading this I'm realising that I don't really know what os.urandom is.
> How exactly is it generating random numbers and what do you mean by it
> running out of entropy?

I am not an expert, but here goes...

Some (most?) modern operating systems provide a cryptographically strong 
source of non-deterministic randomness. The non-deterministic part comes 
from external "stuff", which is called "entropy". Typical sources of 
entropy include network events, user key-presses, moving the mouse, and 
(presumably in machines with special hardware), even thermal noise in 
electrical components.

If the OS hasn't collected enough entropy, urandom can block until it 
has. This can be a problem, e.g. I've experienced issues where scripts 
relying indirectly on urandom that run at system reboot can block for 
minutes at a time, waiting for entropy. If those scripts run before the 
networking software, and before any users log in and start running apps, 
the script can block forever waiting for entropy that never arrives.

(Where "forever" == "70 minute boot times".)

Entropy is used and discarded, so urandom needs the OS to continually 
replenish the amount of entropy. Under normal circumstances, this it 
does, but if you grab lots of urandom output on a system which is 
otherwise quiet and not doing anything, it could run out.

If I've got any of this wrong, corrections will be gratefully acceptable.



-- 
Steven



More information about the Python-list mailing list