Abhishek Pal
Abhishek Pal

Reputation: 141

Running two clojure codes by calling main function sequentially and then communicating between them

I have two clojure namespaces in two different folders with their own main function(since I want to generate classes and eventually create a jar)

(ns reward-module.cli) and (ns nelson_clojure.cli) both of them have their own main functions and so run on separate main threads. I want to send some values from one code to another and vice-versa during runtime. I am confused by the usage and the documentation of channels in clojure.

I assume that when I start a main thread, it is bound to a port and when I start the second thread that is also bound to another port. Is that correct? and if so then how do I communicate between them. Like exchanging values of some variables during runtime.

Do channels in clojure behave like ports? If so then where am I giving the information about the port numbers? How do I get those in the first place? Except looking at the verbose when repl starts...

Upvotes: 0

Views: 153

Answers (2)

rmm
rmm

Reputation: 119

Inside a single JVM process, threads share memory. Clojure's concurrency constructs provide a variety of ways to safely share values in that memory and communicate/coordinate between threads inside the same JVM.

Separate JVM processes (read: operating system process instances) do not share memory, so you'd need to explicitly set up a means for them to communicate, such as via sockets using one of the many established protocols.

Upvotes: 0

bfabry
bfabry

Reputation: 1904

There is only one main thread per process, and processes cannot communicate except through some sort of interprocess-communication. Network, sockets, files, something like that. Clojure channels as in clojure core.async are for communication between threads (or even just for use as queues within a thread) in the same process. I think what you want is to create a separate main function, that creates a thread that runs the function in reward-module.cli, a thread that runs the function in nelson_clojure.cli, and sets up some sort of communication between them, ie with core.async.

The core.async portion of clojure for the brave and true could probably help you out. https://www.braveclojure.com/core-async/

Upvotes: 1

Related Questions