我在 C 中有一个小的数值模拟(我必须用 C 来与我的顾问分享),但我想使用“haskell 脚本”之类的东西来组织模拟。该程序接受一些命令行参数并吐出一些我想重定向到文件的输出,所以我做了这样的事情:
import Control.Monad
import System.Process
我有一个函数来创建输出文件的名称:
filename :: Int -> String
filename n = some stuff here...
和我想运行的命令:
command :: Int -> String
command n = "./mycutesimulation " ++ show n ++ " >" ++ filename n
最后我生成一个我想要运行的列表并使用
runCommand
运行它们:commands = map command [1,2..1000]
main = do
sequence_ $ map runCommand commands
问题是,在我运行这个“脚本”之后,我的计算机几乎因负载而死机。正在执行的程序占用的内存非常少,并且在几分之一秒内运行。这不应该发生。
所以,我的问题是:
1)我是不是刚刚抛出了 1000 个要同时执行的进程???我如何以合理的顺序执行它们 - 按顺序或一次只执行几个进程。
2)我在四核中运行它,使用它对我有利。有没有办法用那个
-threaded
来编译这个?标记并让进程以有组织的方式同时执行?
最佳答案
您需要一个 waitForProcess =<< runCommand
.
import System.Process
main = sequence $ map (\x -> runCommand x) commands
where commands = map (\x -> "echo " ++ show x) [1, 2..1000]
有和你相似的症状,但是
import System.Process
main = sequence $ map (\x -> waitForProcess =<< runCommand x) commands
where commands = map (\x -> "echo " ++ show x) [1, 2..1000]
作品。
关于multithreading - Haskell - 关于 System.Process 和多线程的一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2094056/