-
The example for kademlia (ipfs-kad) shows how to make a request to Kademlia and then enter an event loop to receive the event produced as a result of the request. I am trying to write an application that initiates Kademlia queries while the event loop is running. To do this, the calls to initiate Kademlia queries are running in one tokio task and the event loop is running in another. I would like to see a working example that does this. Here is what I have that does not work. To access the kademlia behaviour to call a The only way I know for two threads to have a mutable reference to a Swarm is for them to get the reference through a mutex. My problem is that this deadlocks. The event loop gets the mutable reference from the mutex and polls the Swarm which has no events, so it waits. The other thread wants to get a mutable reference from the mutex, but never gets it because it is held by the event loop that won't relinquish it until it receives and event. How do I structure something that works? To frame my question in terms of the example in https://github.com/libp2p/rust-libp2p/blob/master/examples/ipfs-kad.rs, how can I change it so that the event loop is started by calling |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Simply use a I have a similar example: gistit-daemon Another similar example: file-sharing |
Beta Was this translation helpful? Give feedback.
Simply use a
mpsc::channel
. The receiver end will be inside your event loop waiting for instructions. (probably inside atokio::select!
)The transmitter you take wherever you want
I have a similar example: gistit-daemon
But I'm using unix sockets here, where you'd be using just a
mpsc::channel
Another similar example: file-sharing