完成最后一条语句后,Clojure程序没有退出吗? [英] Clojure program not exiting when finishing last statement?

查看:70
本文介绍了完成最后一条语句后,Clojure程序没有退出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么会导致Clojure程序在完成主函数中的最后一条语句后不立即退出?

What would cause a Clojure program to not immediately exit upon finishing the last statement in the main function?

我所做的就是更改(doall(map ...))(doall(pmap ...)),程序突然完成后会挂起在退出之前先执行数秒的任务。我会在 -main 函数的最后一行上放置(println Finishing everything!)会打印出来,但仍会退出一段时间。

All I did was to change a (doall (map ...)) to a (doall (pmap ...)), and suddenly the program would hang upon completion of its tasks for a good number of seconds before exiting. I would put a (println "Finished everything!") on the last line of the -main function, and it would print that, yet still not exit for some time. What could cause this behavior, and how should I fix it?

编辑:pmap是程序中唯一被并行化的部分(主要是因为其他所有程序都运行更多或减少瞬间)。由于程序的后半部分需要pmap的所有结果才能正确运行,并且由于map和pmap的程序输出相同,因此我有点怀疑pmap是否仍将在程序末尾运行。将(System / exit 0)放在末尾而不是println也不会更改程序输出。

The pmap is the only part of the program that is parallelized (mostly because everything else runs more or less instantly). As latter parts of the program require all the results from pmap in order to function correctly, and as the program output is the same for both map and pmap, I somewhat doubt that pmap would still be running at the end of the program. Putting (System/exit 0) at the end instead of the println does not change program output, either.

推荐答案

pmap 将旋转线程池中的多个线程以服务您的并发任务。

pmap will spin up multiple threads from a threadpool to service your concurrent tasks.

必须调用shutdown-agents才能使JVM有序退出,因为代理线程池中的线程不是守护程序线程。

Calling shutdown-agents is necessary to allow the JVM to exit in an orderly manner because the threads in the agent threadpools are not daemon threads.

您需要显式告诉他们正常关闭(但仅在程序完成后)。在此处之前,这似乎已经得到了回答。

You need to explicitly tell them to gracefully shutdown (but only when your program is done). This looks to have been answered before here.

引用自答案:

您需要调用 shutdown-agents 杀死支持pmap使用的线程池的线程。

"You need to call shutdown-agents to kill the threads backing the threadpool used by pmap."

文档在这里: http://clojuredocs.org/clojure_core/1.3.0/clojure.core/shutdown-agents

这篇关于完成最后一条语句后,Clojure程序没有退出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆