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

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

问题描述

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

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 "Finished 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天全站免登陆