在Common Lisp中创建进程队列

时间:2015-06-02 05:32:44

标签: common-lisp

我有一台运行Hunchentoot(CentOS和SBCL)的服务器。当用户提交特定类型的发布请求时,将启动子进程(运行程序),这可能需要最多四分钟才能完成。如果五个人同时执行该特定类型的请求,则服务器内存不足并且所有子进程都会中断。您建议使用哪种技术排队流程并一次运行一个流程?

2 个答案:

答案 0 :(得分:2)

您可以设置一个工作线程,该线程接收来自消息队列的指令。我过去曾使用chanl来做类似的事情,但还有其他一些选择。

答案 1 :(得分:0)

(defvar *worker-queue* '())
(defvar *worker-queue-mutex* (sb-thread:make-mutex :name "worker-queue-lock"))
(defvar *worker-queue-semaphore* (sb-thread:make-semaphore :name "worker-queue-semaphore" :count 0))
(defvar *worker-thread*)

(defun worker-queue-function ()
  (sb-thread:with-mutex (*worker-queue-mutex*)
    (let ((popped-worker-queue-item (pop *worker-queue*)))
      (do-something-with popped-worker-queue-item))))

(defun make-worker-thread ()
  (setq *worker-thread* (sb-thread:make-thread (lambda ()
        (loop
          (sb-thread:wait-on-semaphore *worker-queue-semaphore*)
          (worker-queue-function)))
        :name "worker-thread")))

(defun add-item-to-worker-queue (item-to-add-to-worker-queue)
  (sb-thread:with-mutex (*worker-queue-mutex*)
    (setq *worker-queue* (append *worker-queue* (list item-to-add-to-worker-queue)))
    (sb-thread:signal-semaphore *worker-queue-semaphore*))))