【问题标题】:What's the best way to run more than one Process at a time in Haxe?在 Haxe 中一次运行多个进程的最佳方法是什么?
【发布时间】:2016-12-05 23:14:05
【问题描述】:

我最近开始使用 Haxe Process 类。我用它来调用curl 命令。正如您所期望的那样,该过程将阻止程序直到它完成。类似的东西

for( obj in array ) {
 var curl = 'curl "http://something.com?param=$obj"';
 var process = new Process(curl);
 trace('finished);
 process.close();

}

我想做的是一次最多运行 4 或 5 个进程。如果我基本上可以创建一个包含 100 个项目并一次运行多达 4 个进程直到我们到达数组末尾的 for 循环,那就太棒了。

【问题讨论】:

  • 如果马克的回答解决了您的问题,您可能想将其标记为已接受? :)

标签: java c++ haxe


【解决方案1】:

我认为您需要研究线程。我发现这个示例可能会有所帮助,但不确定您如何在 Haxe/Java 中处理此问题

#if cpp
    import cpp.vm.Thread;
    import cpp.vm.Deque;
#elseif neko
    import neko.vm.Thread;
    import neko.vm.Deque;
#end

/** 
A simple Haxe class for easily running threads and calling functions on the primary thread.
from https://github.com/underscorediscovery/
*/
class Runner {

    public static var primary : Thread;

    static var queue : Deque<Void->Void>;

        /** Call this on your thread to make primary,
            the calling thread will be used for callbacks. */
    public static function init() {
        queue = new Deque<Void->Void>();
        primary = Thread.current();
    }

        /** Call this on the primary manually,
            Returns the number of callbacks called. */
    public static function run() : Int {

        var more = true;
        var count = 0;

        while(more) {
            var item = queue.pop(false);
            if(item != null) {
                count++; item(); item = null;
            } else {
                more = false; break;
            }
        }

        return count;

    } //process

        /** Call a function on the primary thread without waiting or blocking.
            If you want return values see call_primary_ret */
    public static function call_primary( _fn:Void->Void ) {

        queue.push(_fn);

    } //call_primary

        /** Call a function on the primary thread and wait for the return value.
            This will block the calling thread for a maximum of _timeout, default to 0.1s.
            To call without a return or blocking, use call_primary */
    public static function call_primary_ret<T>( _fn:Void->T, _timeout:Float=0.1 ) : Null<T> {

        var res:T = null;
        var start = haxe.Timer.stamp();
        var lock = new cpp.vm.Lock();

            //add to main to call this
        queue.push(function() {
            res = _fn();
            lock.release();
        });

            //wait for the lock release or timeout
        lock.wait(_timeout);

            //clean up
        lock = null;
            //return result
        return res;

    } //call_primary_ret

        /** Create a thread using the given function */
    public static function thread( fn:Void->Void ) : Thread {
        return Thread.create( fn );
    }

} //Runner

来源: https://gist.github.com/underscorediscovery/e66e72ec702bdcedf5af45f8f4712109

【讨论】:

    猜你喜欢
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    相关资源
    最近更新 更多