Is calling the bitcoind RPC multi-thread safe?

3

1

My application uses bitcoind RPC with a Python wrapper. I want to write one thread that periodically watches a number of addresses by calling "listtransactions". Other threads might need to call other API commands in the meantime.

Is it safe to use bitcoind in such a multi-thread setup? (maybe I will use multi-processing in the end because of Pythons global interpreter lock).

When I searched for an answer I found:

Are bitcoin commands resistant to race conditions and thread-safe?

But I am not sure if this answer applies to my problem (and I am also not sure whether Pieter's "yes" is a reply to the first or the second question).

Thanks.

Marco Polo

Posted 2013-10-31T15:55:35.643

Reputation: 90

Answers

3

It can be done safely but it can also be done unsafely. If each thread uses its own connection, then it is safe. If the threads "fight" over the connections, then it won't work.

David Schwartz

Posted 2013-10-31T15:55:35.643

Reputation: 46 931

Thanks. That is very helpful. I will use then

conn = bitcoinrpc.connect_to_local()

in each thread, to ensure each thread uses its own connection. – Marco Polo 2013-11-01T17:28:22.430

2

Rather than polling for transactions, you can use pynode or bitcoin-sniffer to connect to the node directly and parse transactions as they happen. If the addresses are in your wallet, you can call bitcoind with the walletnotify option set, which will execute your command with the details of the new transaction.

Anonymous

Posted 2013-10-31T15:55:35.643

Reputation: 31

Very useful links. Seems to be a lot easier to use than polling. Many thanks!Marco Polo 2013-11-03T10:06:10.960