1

I was looking at ../qa/rpc-tests/util.py in order to figure out how arbitrary ports are assigned to each created node. In the first lines of the code it says:

...
def p2p_port(n):
return 11000 + n + os.getpid()%999
...

So I'm wondering what exactly os.getpid()%999 does? Is it instantly looking for a free port?

Thanks in advance!

3

os.getpid() in Python is getting the current process id. The processor id is 'random-ish', in that running the entire test several times will result in a different pid, but within the same test the pid will remain constant.

The % operator is the modulus operator, which is essentially chopping off all but the last 3 digits of the processor id.

Together os.getpid() % 999 is getting a constant offset and ignoring all but the last 3 digits. This new 3 digit number of added to the constant 11000 to return the port number to use.

  • Ok, but what I don't understand is how a pid can be set inside a configuration file for a process that is non-existent yet? Can you follow my thought? :/ – Aliakbar Ahmadi Apr 15 '15 at 15:57
  • 1
    The pid that os.getpid() returns is the pid of the Python script at the time that it is being run. Whenever p2p_port(n) is called, the id of the process that is currently running is returned. Is that more clear? – JohnDvorak Apr 15 '15 at 16:01
  • 1
    Looking at the code now, it seems like util.py just includes functions that are used by other portions of the test code. So every time other portions of the code call initialize_datadir() and initialize_chain() (which then call p2p_port()), the pids will be the same if they are called within the same process. – JohnDvorak Apr 15 '15 at 16:14
  • 1
    I think the source of confusion is that os.getpid() is not the process id of bitcoind, but of the Python script. It has nothing to do with the bitcoind pid, and is only a way to make the port different between tests. – JohnDvorak Apr 15 '15 at 22:36
  • 1
    Glad I helped! :) – JohnDvorak Apr 16 '15 at 19:49

Your Answer

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

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