monero-ecosystem / monero-python

A comprehensive Python module for handling Monero cryptocurrency
BSD 3-Clause "New" or "Revised" License
245 stars 79 forks source link

Is monero-python thread safe? #41

Closed kim0 closed 5 years ago

kim0 commented 5 years ago

I'm building an API, and I notice sometimes acc.transfer() takes more than one full minute to finish! Ofc, this is way too long. What is your advice to best handle multiple concurrent API callers? Should I go threads way? is Monero-Python thread safe to begin with ?

emesik commented 5 years ago

Hi, To the best of my knowledge, the RPC backend (monero-wallet-rpc) is single-threaded which means that while handling your first request it will not accept another. (You'd better check it, though. I'd be very interested in results too.)

To avoid that limitation, you could run two instances of monero-wallet-rpc on separate wallet copies, and handle them in threads with separate Wallet objects, but that's only a partial solution of the problem. If you try to send concurrent transactions from those wallets, they will use the same outputs pretty soon. Such race condition will have one of the transactions rejected as double spend. Effectively, you'd have to make sure only one wallet is performing transfers, while the other is for read-only operations. I guess that's not a satisfactory solution.

Right now I guess it's best to look why acc.transfer() takes so long. Perhaps your wallet is fragmented into many small outputs? If that's accidental, a well-planned churn might help. Or maybe the machine running the wallet is just slow?

Or perhaps you meant acc.transfer_multiple() and you tried to handle significant number of recipients at once? That might also be slow.

kim0 commented 5 years ago

Alright thanks .. I am running wallet-rpc on my macbook (SSD), connected to xmr.to node (stagenet). Perhaps the high latency (100ms) between me and the node is the root cause here. Not sure how "chatty" wallet-rpc is. But if it won't be that bad in production (latency order of magnitude better), then I guess I'm mostly Ok!

For concurrency, I filed https://github.com/monero-project/monero/issues/5077

emesik commented 5 years ago

That is very likely the reason. Remember that the wallet queries blockchain for decoys to each input when constructing transactions. It takes long time over slow connection. Definitely you should run own node, also for development.