os.getpid() in Bitcoin's python test scripts - What does it actually do?

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!

Aliakbar Ahmadi

Posted 2015-04-15T11:25:00.960

Reputation: 1 335

Answers

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.

JohnDvorak

Posted 2015-04-15T11:25:00.960

Reputation: 627

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 2015-04-15T15:57:48.150

1The 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 2015-04-15T16:01:42.490

Yes a little bit, since I'm a newbie in this field. So you say everytime this python script is run, it starts many process instances (nodes) of the python programm (not bitcoind!) that have individual process ids?Aliakbar Ahmadi 2015-04-15T16:05:50.673

1Looking 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 2015-04-15T16:14:43.473

Exactly initialize_datadir() is the point of confusion. My understanding/pattern of creating a network of peers is the following:Aliakbar Ahmadi 2015-04-15T21:40:03.853

Step 1) create config file with free port and rpc portAliakbar Ahmadi 2015-04-15T21:40:37.053

Step 2) run bitcoind instance separately by specifying independent config files - running bitcoin instantly gets a process idAliakbar Ahmadi 2015-04-15T21:41:43.073

BUT in the util.py code it seems to me that step1 and 2 are twisted since for writing a config file a pid for the port no. Structure is neededAliakbar Ahmadi 2015-04-15T21:43:23.827

1I 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 2015-04-15T22:36:01.847

Ok. Thanks a lot I did a bit of research basing on your hints so it's much clearer now!Aliakbar Ahmadi 2015-04-16T19:47:51.787

1Glad I helped! :)JohnDvorak 2015-04-16T19:49:02.047