执行:显示stdout“活”(Exec : display stdout “live”)
我有这个简单的脚本:
var exec = require('child_process').exec; exec('coffee -cw my_file.coffee', function(error, stdout, stderr) { console.log(stdout); });
在那里我只需执行一个命令来编译一个咖啡脚本文件。 但是stdout不会在控制台中显示,因为命令永远不会结束(因为-w选项的咖啡)。 如果我直接从控制台执行命令,我会得到如下消息:
18:05:59 - compiled my_file.coffee
我的问题是:可以用node.js exec显示这些消息吗? 如果是的话, !
谢谢
I have this simple script :
var exec = require('child_process').exec; exec('coffee -cw my_file.coffee', function(error, stdout, stderr) { console.log(stdout); });
where I simply execute a command to compile a coffee-script file. But stdout never get displayed in the console, because the command never ends (because of the -w option of coffee). If I execute the command directly from the console I get message like this :
18:05:59 - compiled my_file.coffee
My question is : is it possible to display these messages with the node.js exec ? If yes how ? !
Thanks
原文:https://stackoverflow.com/questions/10232192
最满意答案
在最新版本的GHC中,可以使用
setAllocationCounter
和enableAllocationLimit
来设置每个线程的分配计数器和限制。 当限制被设置并且计数器达到0时,线程接收异步异常。计数器测量分配 ,而不是现场组的大小。 例如,这个代码达到了极限,尽管它的现场设置永远不会变得很大:
{-# LANGUAGE NumDecimals #-} module Main where import Data.Foldable (for_) import System.IO import GHC.Conc (setAllocationCounter,enableAllocationLimit) main :: IO () main = do setAllocationCounter 2e9 enableAllocationLimit let writeToHandle h = for_ ([1..]::[Integer]) (hPutStrLn h . show) withFile "/dev/null" WriteMode writeToHandle return ()
分配有点粗糙,但它仍然可以用来检测一些“失控”的计算。
Simon Marlow的这篇博客更详细地介绍了这一点。
In the latest versions of GHC, it is possible to set per-thread allocation counters and limits, using
setAllocationCounter
andenableAllocationLimit
fromGHC.Conc
. When a limit is set and the counter reaches 0, the thread receives an asynchronous exception.The counters measure allocation, and not the size of the live set. For example, this code hits the limit, despite its live set never becoming very big:
{-# LANGUAGE NumDecimals #-} module Main where import Data.Foldable (for_) import System.IO import GHC.Conc (setAllocationCounter,enableAllocationLimit) main :: IO () main = do setAllocationCounter 2e9 enableAllocationLimit let writeToHandle h = for_ ([1..]::[Integer]) (hPutStrLn h . show) withFile "/dev/null" WriteMode writeToHandle return ()
Allocation is a bit crude as a measure, but it can still be useful to detect some "out of control" computations.
This blog post by Simon Marlow goes into more detail.
相关问答
更多-
单独使用monad创建一个不unsequence函数是不可能的。 原因是: 您可以使用return方便而安全地轻松创建一个monadic结构。 但是,从monadic结构中删除值并不安全。 例如,你不能从空列表中删除一个元素(即类型[a] -> a函数不安全)。 因此,我们有一个特殊的函数(即>>= ),它可以安全地从一元结构(如果存在的话)中去除一个值,对它进行处理并返回另一个安全的一元结构。 因此,从一个值创建一个monadic结构是安全的。 但是,从一元结构中删除值并不安全。 假设我们有一个函数ext ...
-
我可以限制Haskell中每个函数/ monad /线程的内存使用情况吗?(May I limit memory usage per function/monad/thread in Haskell?)[2021-11-09]
在最新版本的GHC中,可以使用setAllocationCounter和enableAllocationLimit来设置每个线程的分配计数器和限制。 当限制被设置并且计数器达到0时,线程接收异步异常。 计数器测量分配 ,而不是现场组的大小。 例如,这个代码达到了极限,尽管它的现场设置永远不会变得很大: {-# LANGUAGE NumDecimals #-} module Main where import Data.Foldable (for_) import System.IO import GHC. ... -
正如Pipes.foldl的文档中所提到的,折叠是严格的。 但是,严格是用$!实现的$! 这只会迫使对WHNF进行评估 - 弱头正常形式。 WHNF足以完全评估像Int这样的简单类型,但它不足以完全评估像Maybe Int这样的更复杂的类型。 一些例子: main1 = do let a = 3 + undefined b = seq a 10 print b -- error: Exception: Prelude.undefined main2 = d ...
-
一种简单的方法是使用-rtsopts进行编译 ghc -O2 -rtsopts Test.hs -o test 然后运行它 ./test +RTS -s 如果你需要实际的分析,你可以用-prof编译并运行-p - 这将生成一个.prof文件,然后你可以检查 (参见分析用户指南 ) one easy way is to compile with -rtsopts ghc -O2 -rtsopts Test.hs -o test and then run it with ./test +RTS -s i ...
-
在monad中的字段。(Field in monad. Haskell)[2022-04-05]
我想你可能想要这样的东西: nextStatement :: MonadError String m => Stmts -> m Stmt nextStatement (Statements s _) = return s nextStatement EmptyStmts = throwError "nextStatement EmptyStmts" 特别是Interpreter是MonadError String一个实例,所以这也可以给出类型Stmts -> Interpreter Stmt 。 你可以 ... -
问题在于你实际上并没有在foo里面调用 bar 。 你可以使用_ <- bar来做到这一点。 这附加"foobar"的状态: foo :: MyStateMonadT Bar -> MyStateMonadT Foo foo bar = do modify (++"foo") _ <- bar return Foo 这附加"barfoo" : foo :: MyStateMonadT Bar -> MyStateMonadT Foo foo bar = do _ <- bar modi ...
-
类型构造函数是返回类型的类型级函数。 Maybe是一个类型构造函数,它接受一个单一的类型参数并返回一个类型,例如Maybe String , Maybe Int等。 数据构造函数用于创建特定类型的值。 对于某些类型Maybe a这些构造函数是Just和Nothing ie data Maybe a = Just a | Nothing return函数从“简单”值构造一个monadic值,例如 return 1 :: Maybe Int return "s" :: [String] 所以在Monad类的 ...
-
也许monad使用示例(Maybe monad usage example)[2021-12-31]
对于Maybe monad bind函数( >>= )看起来像这样: (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b 所以,我们来定义一些Maybe a值: > let a = Just 1 a :: Maybe Integer 和:: a -> Maybe b函数: > let f = \x -> Just (x+1) f :: Integer -> Maybe Integer 现在我们可以使用像中缀运算符那样的绑定: > a >>= f Just 2 i ... -
Haskell / GHC内存使用情况(Haskell/GHC memory usage)[2020-02-29]
编译而不是从ghci中运行。 在我的机器上,这使得virt从5GB到140MB; 编译版本的资源永远不会超过15MB。 一般情况下(除了极少数例外),ghci的性能特征要比编译的程序差很多。 Compile instead of running from within ghci. On my machine this took virt usage from 5GB to 140MB; res never went above about 15MB for the compiled version. In ... -
理解状态monad的最简单方法,我认为只是编写你自己的并且稍微玩一下。 学习这些代码,与其他人的例子一起玩,然后不时回顾一下,直到你能够从内存中写下来: -- | 'State' is just a newtype wrapper around the type @s -> (a, s)@. -- These are functions which are fed a state value (type @s@) as input, -- and produce as a pair of an @a@ (t ...