terminating an app that uses agents

135 views
Skip to first unread message

Mark Volkmann

unread,
Jan 5, 2009, 9:35:04 PM1/5/09
to clo...@googlegroups.com
Why doesn't the following code terminate on its own? It outputs the
number 6 as expected, but then just hangs. I'm guessing its related to
non-daemon threads related to agents that are still running. What's
the proper way to exit?

(def my-agent (agent 1))

(defn sleep-and-multiply [old-state times ms]
(Thread/sleep ms)
(* old-state times))

(send-off my-agent sleep-and-multiply 2 1500)
(send-off my-agent sleep-and-multiply 3 1000)
(await my-agent)
(println "my-agent =" @my-agent)

--
R. Mark Volkmann
Object Computing, Inc.

Christian Vest Hansen

unread,
Jan 6, 2009, 2:58:02 AM1/6/09
to clo...@googlegroups.com
You need to remember to ...

clojure.core/shutdown-agents
([])
Initiates a shutdown of the thread pools that back the agent
system. Running actions will complete, but no new actions will be
accepted
nil
--
Venlig hilsen / Kind regards,
Christian Vest Hansen.

wubbie

unread,
Jan 6, 2009, 11:00:56 AM1/6/09
to Clojure
Hi,

I tried your example and some weird sequence generates
java.util.concurrent.RejectedExecutionException (NO_SOURCE_FILE:0)


Here is the scenario:
Clojure
user=> (def counter (agent 0))
#'user/counter
user=> (send counter inc)
#<Agent clojure.lang.Agent@9e4585>
user=> @counter
1
user=> (def my-agent (agent 1))
#'user/my-agent
user=> (defn sleep-and-multiply [old-state ms]
(Thread/sleep ms)
(* old-state times))
java.lang.Exception: Unable to resolve symbol: times in this context
(NO_SOURCE_FILE:7)
user=> ;
(defn sleep-and-multiply [old-state n ms]
(Thread/sleep ms)
(* old-state n))
#'user/sleep-and-multiply
user=> (send-off my-agent sleep-and multiply 2 1500)
java.lang.Exception: Unable to resolve symbol: sleep-and in this
context (NO_SOURCE_FILE:12)
user=> (send-off my-agent sleep-and- multiply 2 1500)
java.lang.Exception: Unable to resolve symbol: sleep-and- in this
context (NO_SOURCE_FILE:13)
user=> (send-off my-agent sleep-and-multiply 2 1500)
#<Agent clojure.lang.Agent@9a99eb>
user=> (send-off my-agent sleep-and-multiply 3 1500)
#<Agent clojure.lang.Agent@9a99eb>
user=> (await my-agent)
nil
user=> (println "my-agent=" @my-agent)
my-agent= 6
nil
user=> (shutdown-agents)
nil
user=> (def my-agent (agent 10))
#'user/my-agent
user=> (defn sleep-and-multiply [old-state n ms]
(Thread/sleep ms)
(+ old-state n))
#'user/sleep-and-multiply
user=> (send-off my-agent sleep-and-multiply 7 1500)
java.util.concurrent.RejectedExecutionException (NO_SOURCE_FILE:0)
user=> (send-off my-agent sleep-and-multiply 3 1500)
#<Agent clojure.lang.Agent@3a5a9c>
user=> (send-off my-agent sleep-and-multiply 7 1500)
#<Agent clojure.lang.Agent@3a5a9c>
user=> (println "my-agent = " @my-agent)
my-agent = 10
nil
user=> (agent-errors my-agent)
nil
user=> (clear-agent-errors my-agent)
nil
user=> (println "my-agent = " @my-agent)
my-agent = 10
nil
user=> (send-off my-agent sleep-and-multiply 7 1500)
#<Agent clojure.lang.Agent@3a5a9c>
user=> (println "my-agent = " @my-agent)
my-agent = 10
nil
user=> (send-off my-agent sleep-and-multiply 3 1500)
#<Agent clojure.lang.Agent@3a5a9c>
user=> (println "my-agent = " @my-agent)
my-agent = 10
nil

Sun

Mark Volkmann

unread,
Jan 6, 2009, 11:18:54 AM1/6/09
to clo...@googlegroups.com
On Tue, Jan 6, 2009 at 10:00 AM, wubbie <sun...@gmail.com> wrote:
>
> Hi,
>
> I tried your example and some weird sequence generates
> java.util.concurrent.RejectedExecutionException (NO_SOURCE_FILE:0)
>
>
> Here is the scenario:
> Clojure
> user=> (def counter (agent 0))
> #'user/counter
> user=> (send counter inc)
> #<Agent clojure.lang.Agent@9e4585>
> user=> @counter
> 1

The part above isn't related to my example.

> user=> (def my-agent (agent 1))
> #'user/my-agent

> user=> (defn sleep-and-multiply [old-state ms]
> (Thread/sleep ms)
> (* old-state times))
> java.lang.Exception: Unable to resolve symbol: times in this context
> (NO_SOURCE_FILE:7)

This is missing the "times" parameter.

> user=> ;
> (defn sleep-and-multiply [old-state n ms]
> (Thread/sleep ms)
> (* old-state n))
> #'user/sleep-and-multiply

This looks fine. You're just naming the parameter "n" instead of
"times", but it works the same.

> user=> (send-off my-agent sleep-and multiply 2 1500)
> java.lang.Exception: Unable to resolve symbol: sleep-and in this
> context (NO_SOURCE_FILE:12)

You're missing a hyphen in "sleep-and-multiply".

> user=> (send-off my-agent sleep-and- multiply 2 1500)
> java.lang.Exception: Unable to resolve symbol: sleep-and- in this
> context (NO_SOURCE_FILE:13)

You've got a space in "sleep-and-multiply".

> user=> (send-off my-agent sleep-and-multiply 2 1500)
> #<Agent clojure.lang.Agent@9a99eb>
> user=> (send-off my-agent sleep-and-multiply 3 1500)
> #<Agent clojure.lang.Agent@9a99eb>
> user=> (await my-agent)
> nil
> user=> (println "my-agent=" @my-agent)
> my-agent= 6
> nil
> user=> (shutdown-agents)
> nil

All this is correct!

> user=> (def my-agent (agent 10))

Redefining the agent to start with a different value.

> #'user/my-agent
> user=> (defn sleep-and-multiply [old-state n ms]
> (Thread/sleep ms)
> (+ old-state n))
> #'user/sleep-and-multiply

Why are you defining that function again?
Oh I see. You're using addition instead of multiplication.

> user=> (send-off my-agent sleep-and-multiply 7 1500)
> java.util.concurrent.RejectedExecutionException (NO_SOURCE_FILE:0)

I don't know why you're getting that error.

> user=> (send-off my-agent sleep-and-multiply 3 1500)
> #<Agent clojure.lang.Agent@3a5a9c>
> user=> (send-off my-agent sleep-and-multiply 7 1500)
> #<Agent clojure.lang.Agent@3a5a9c>
> user=> (println "my-agent = " @my-agent)
> my-agent = 10
> nil

Yikes! I'm sure you expected the result to be 20. The value didn't get
changed! I don't know why.

> user=> (agent-errors my-agent)
> nil
> user=> (clear-agent-errors my-agent)
> nil
> user=> (println "my-agent = " @my-agent)
> my-agent = 10
> nil
> user=> (send-off my-agent sleep-and-multiply 7 1500)
> #<Agent clojure.lang.Agent@3a5a9c>
> user=> (println "my-agent = " @my-agent)
> my-agent = 10
> nil
> user=> (send-off my-agent sleep-and-multiply 3 1500)
> #<Agent clojure.lang.Agent@3a5a9c>
> user=> (println "my-agent = " @my-agent)
> my-agent = 10
> nil

Does anyone know why the value of the agent isn't getting changed?

Chouser

unread,
Jan 6, 2009, 11:46:02 AM1/6/09
to clo...@googlegroups.com
On Tue, Jan 6, 2009 at 11:18 AM, Mark Volkmann
<r.mark....@gmail.com> wrote:
>
> On Tue, Jan 6, 2009 at 10:00 AM, wubbie <sun...@gmail.com> wrote:
>> user=> (shutdown-agents)
>> nil
>
[snip]

>
>> user=> (send-off my-agent sleep-and-multiply 7 1500)
>> java.util.concurrent.RejectedExecutionException (NO_SOURCE_FILE:0)
>
> I don't know why you're getting that error.

The error is because you shut down the agent pools. Don't do that
until you're done sending actions to agents!

--Chouser

wubbie

unread,
Jan 6, 2009, 12:07:04 PM1/6/09
to Clojure
But after I shutdown the agent,
I created a new agent with the same name and also defined the
function.
So shutdown on earlier agent should not matter?

-Sun

On Jan 6, 11:46 am, Chouser <chou...@gmail.com> wrote:
> On Tue, Jan 6, 2009 at 11:18 AM, Mark Volkmann
>
> <r.mark.volkm...@gmail.com> wrote:

Mark Volkmann

unread,
Jan 6, 2009, 12:25:09 PM1/6/09
to clo...@googlegroups.com
On Tue, Jan 6, 2009 at 11:07 AM, wubbie <sun...@gmail.com> wrote:
>
> But after I shutdown the agent,
> I created a new agent with the same name and also defined the
> function.
> So shutdown on earlier agent should not matter?

I think he means that once you shut it down you can't use send or
send-off for any agent for the rest of run of the application ... or
the rest of the REPL session.

> -Sun
>
> On Jan 6, 11:46 am, Chouser <chou...@gmail.com> wrote:
>> On Tue, Jan 6, 2009 at 11:18 AM, Mark Volkmann
>>
>> <r.mark.volkm...@gmail.com> wrote:
>>
>> > On Tue, Jan 6, 2009 at 10:00 AM, wubbie <sunj...@gmail.com> wrote:
>> >> user=> (shutdown-agents)
>> >> nil
>>
>> [snip]
>>
>> >> user=> (send-off my-agent sleep-and-multiply 7 1500)
>> >> java.util.concurrent.RejectedExecutionException (NO_SOURCE_FILE:0)
>>
>> > I don't know why you're getting that error.
>>
>> The error is because you shut down the agent pools. Don't do that
>> until you're done sending actions to agents!
>>
>> --Chouser
> >
>



Reply all
Reply to author
Forward
0 new messages