with-timeout示例用法(基元可以等待什么?)

时间:2012-06-16 21:53:50

标签: emacs timer

文档说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)))))))

1 个答案:

答案 0 :(得分: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)))