4

ドキュメントには、with-timeoutマクロが内部で実行するコードが「待機できるプリミティブである」場合にのみ待機できると記載されています。そのようなプリミティブの例はありますか?

詳細情報: TCP サーバーへの非同期呼び出しを行う関数から結果を返さなければならない状況があります。したがって、この呼び出しを同期させる方法を知っていれば、それも役に立ちます。

残念ながら、同期的に行うことはできません。これはオートコンプリート ライブラリによって呼び出されるコールバックであり、値を返す必要があります。以下は、それを実行しようとするコードです。

(defun haxe-ac-init ()
  (unless (get-process haxe-compiler-process)
    (haxe-connector-sentinel nil nil))
  (let ((ac-request
         (haxe-build-compile-string
          (haxe-package) (buffer-file-name))))
    (save-buffer)
    (setq haxe-last-ac-candidates nil)
    (process-send-string haxe-compiler-process ac-request)
    (process-send-string haxe-compiler-process "\0")
    (process-send-eof haxe-compiler-process))
  (haxe-log 3 "haxe-ac-init sent request.")
  (with-local-quit
    (with-timeout (3 (haxe-log 0 "Compiler is too slow..."))
      (block x ;; this while sometimes will loop forever
        (while (equal (process-status (get-process haxe-compiler-process)) 'open)
          (when (and last-compiler-response (= received-status 2))
            (haxe-parse-ac-response last-compiler-response)
            (throw 'x haxe-last-ac-candidates)))))))
4

1 に答える 1

2

私自身の質問に答えなければなりませんでした、それは簡単ではありませんでした!:)それで...が機能するためにwith-timeout、私の場合、私はaccept-process-outputループに入れなければなりませんでした。これにより、Emacsはループを中断し、書き込みを待機しているプロセスから読み取り、ループに戻ることができるようになります。コードの最終バージョンは次のとおりです。

(defun haxe-ac-init ()
  (message "haxe-ac-init 0 %s" (get-process haxe-compiler-process))
  (let ((old-proc (get-process haxe-compiler-process)))
    ;; (message "haxe-ac-init 1 %s %s" old-proc (when old-proc (process-status old-proc)))
    ;; process-live-p doesn't exist :/
    (when (or (not old-proc)
              (not (equal (process-status old-proc) 'open)))
      (setq haxe-network-process nil)
      (haxe-connect-to-compiler-server)
      (sleep-for 1)
      (setq old-proc (get-process haxe-compiler-process)))
    (let ((ac-request
           (haxe-build-compile-string
            (haxe-package) (buffer-file-name))))
      (setq haxe-last-ac-candidates nil
            haxe-last-ac-candidates-filtered nil
            last-compiler-response nil
            received-status 2)
      (clrhash documentation-hash)
      (process-send-string old-proc ac-request)
      (process-send-string old-proc "\0")
      (process-send-eof old-proc)
      (haxe-log 3 "haxe-ac-init sent request: %s\n completing: %s"
                ac-request
                (substring (buffer-string)
                           (max (point-min) (- (point) 10))
                           (point))))
    (with-local-quit
      (with-timeout (5 (haxe-log 0 "Failing to fetch all completion options, giving up"))
        (while (not haxe-last-ac-candidates)
          (accept-process-output old-proc)
          (haxe-log 3 "statsus: %s"
                    (when last-compiler-response
                      (concat
                       (substring last-compiler-response
                                  0 (min (length last-compiler-response) 42)) "...")))
          (when (and last-compiler-response (= received-status 2))
            (if (string= response-terminator "</list>\n")
                (haxe-parse-ac-response last-compiler-response)
              (haxe-parse-hint-response last-compiler-response)))))
      haxe-last-ac-candidates)))
于 2012-06-17T20:24:54.433 に答える