我正在尝试使用 clojurepantomime
库从大量tif
文档(以及其他文档)中提取/ocr 文本。
我的计划是使用 pmap 对一系列输入数据(来自 postgres 数据库)应用映射,然后使用 tika/tesseract OCR 输出更新同一个 postgres 数据库。这一直工作正常,但是我在 htop 中注意到许多内核有时处于空闲状态。
无论如何要调和这一点,我可以采取哪些步骤来确定为什么这可能会在某处阻塞?所有处理都发生在一个 tif 文件上,并且每个线程都是完全互斥的。
附加信息:
- 一些 tika/tesseract 过程需要 3 秒,其他需要 90 秒。一般来说,tika 受 CPU 限制很大。根据 ,我有足够的可用内存
htop
。 - postgres 在会话管理中没有锁定问题,所以我认为这不会阻碍我。
- 也许某个地方
future
正在等待deref
?怎么知道在哪里?
任何提示表示赞赏,谢谢。下面添加的代码。
(defn parse-a-path [{:keys [row_id, file_path]}]
(try
(let [
start (System/currentTimeMillis)
mime_type (pm/mime-type-of file_path)
file_content (-> file_path (extract/parse) :text)
language (pl/detect-language file_content)
]
{:mime_type mime_type
:file_content file_content
:language language
:row_id row_id
:parse_time_in_seconds (float (/ ( - (System/currentTimeMillis) start) 100))
:record_status "doc parsed"})))
(defn fetch-all-batch []
(t/info (str "Fetching lazy seq. all rows for batch.") )
(jdbc/query (db-connection)
["select
row_id,
file_path ,
file_extension
from the_table" ]))
(defn update-a-row [{:keys [row_id, file_path, file_extension] :as all-keys}]
(let [parse-out (parse-a-path all-keys )]
(try
(doall
(jdbc/execute!
(db-connection)
["update the_table
set
record_last_updated = current_timestamp ,
file_content = ? ,
mime_type = ? ,
language = ? ,
parse_time_in_seconds = ? ,
record_status = ?
where row_id = ? "
(:file_content parse-out) ,
(:mime_type parse-out) ,
(:language parse-out) ,
(:parse_time_in_seconds parse-out) ,
(:record_status parse-out) ,
row_id ])
(t/debug (str "updated row_id " (:row_id parse-out) " (" file_extension ") "
" in " (:parse_time_in_seconds parse-out) " seconds." )))
(catch Exception _ ))))
(dorun
(pmap
#(try
(update-a-row %)
(catch Exception e (t/error (.getNextException e)))
)
fetch-all-batch )
)