- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 subprocess
运行的进程模块的Popen
:
result = subprocess.Popen(['tesseract','mypic.png','myop'])
st = time()
while result.poll() is None:
sleep(0.001)
en = time()
print('Took :'+str(en-st))
Took :0.44703030586242676
tesseract
调用处理图像
mypic.png
(附件)并将 OCR 的结果输出到
myop.txt
.
lst = []
for i in range(4):
lst.append(subprocess.Popen(['tesseract','mypic.png','myop'+str(i)]))
i=0
l = len(lst)
val = 0
while(val!=(1<<l)-1):
if(lst[i].poll() is None):
print('Waiting for :'+str(i))
sleep(0.01)
else:
temp = val
val = val or (1<<(i))
if(val!=temp):
print('Completed for :'+temp)
i = (i+1) %l
tesseract
进行 4 次调用。 , 将进程对象保存在列表中
lst
,遍历所有这些对象,直到
全部 其中完成。底部给出了无限循环的实现说明。
poll()
持续等待进程完成。函数,即
None
直到该过程尚未完成。这不应该发生。它应该只用了 0.44 秒多一点。不是10分钟!为什么会这样?
pytesseract
发现了这个特定错误。 ,在使用
multiprocessing
并行运行时会花费大量时间或
pathos
.所以这是一个更大问题的缩小版本。我的问题可以找到
here .
val
最初为 0。它与
2^i
进行或运算当第 i 个过程完成时。因此,如果有 3 个进程,那么如果第一个进程(i=0)完成,那么
2^0 = 1
与
val
进行或运算1. 完成第二和第三道工序后,
val
变成
2^0
|
2^1
|
2^2
= 7. 和
2^3-1
也是 7。所以循环一直工作到
val
等于
2^{number of processes}-1
.
最佳答案
根据 the faq (我强调):
Tesseract 4 also uses up to four CPU threads while processing a page, so it will be faster than Tesseract 3 for a single page.
If your computer has only two CPU cores, then running four threads will slow down things significantly and it would be better to use a single thread or maybe a maximum of two threads! Using a single thread eliminates the computation overhead of multithreading and is also the best solution for processing lots of images by running one Tesseract process per CPU core.
Set the maximum number of threads using the environment variable OMP_THREAD_LIMIT.
To disable multithreading, use OMP_THREAD_LIMIT=1.
import subprocess
import time
import os
t = time.perf_counter()
tasks = [('mypic.png', 'myop{}'.format(i)) for i in range(4)]
procs = [subprocess.Popen(['tesseract', infile, outfile], env={'OMP_THREAD_LIMIT':'1'})
for infile, outfile in tasks]
for proc in procs:
proc.wait()
print('{} s'.format(time.perf_counter()-t))
在 0.220 秒内完成,而没有
env={'OMP_THREAD_LIMIT':'1'}
的相同代码
|
而不是
logical or operator, or
:
val = val | (1 << (i))
例如,
import time
import subprocess
lst = []
for i in range(4):
lst.append(subprocess.Popen(['tesseract', 'mypic.png', 'myop'+str(i)]))
i = 0
l = len(lst)
val = 0
counter = 0
while(val != (1 << l)-1):
if(lst[i].poll() is None):
time.sleep(0.001)
else:
temp = val
val = val | (1 << (i))
if(val != temp):
print('Completed for : {}'.format(i))
i = (i+1) % l
counter += 1
print('{} iterations'.format(counter))
打印输出如
Completed for : 1
Completed for : 2
Completed for : 3
Completed for : 0
6121 iterations
注意循环仍然迭代数千次,主要是在
lst[i].poll()
时。返回
None
,
i = (i+1) % l
可以多次重访同一个值。
while
循环很复杂而且不是很快。
关于python - 多个子流程需要很长时间才能完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53838992/
这个问题在这里已经有了答案: Isn't an Int64 equal to a long in C#? (2 个答案) 关闭 9 年前。 它不应该是一个整数类型吗? 这样,一些使用 int 的函数
当我遇到一些我想知道的事情时,我正忙着解析一个二进制文件。 Stream.Position属性的类型为Int64或long。为什么是这样?因为流中的位置不能为负,所以使用 UInt64 不是更有意义吗
所以第一部分是我从用户那里获得输入,在本例中,输入是“1”作为从另一个函数接收的字符值。 printf ("\nPlease enter 1, 2, 3 or q: "); option =
我正在尝试使用以下代码从 REST 服务返回 JSON: $categories = $categoriesController->listAll(); if($categories){ hea
我阅读了文档,它说 long 是 %li,但打印输出返回为 -2147024891。是什么赋予了? 最佳答案 您甚至没有提供要打印的号码,但我猜您已经无意中发现了签名打印和未签名打印之间的区别。 使用
我正在创建自定义购物车,我正在构建一个查询,该查询从检索我刚刚保存到购物车表中的 session_id 开始。我知道这个值被保存了,我在 mysql 命令行运行这个查询,它返回我需要的但我没有将值放入
我有一个包含 textView 的 scrollView。如果文本很长并且不适合屏幕,我想增加 textView 高度(我想我可以通过添加 NSLayoutConstraint outlet 并修改它
我有一个基本的数据库处理程序类,其中有一个使用 PDO::FETCH_ASSOC 参数返回结果集的公共(public)方法: public function resultSet() { $th
在后台线程中,我调用 PublishSubject.onNext(); 并在主线程中通过 subscribe(PublishSubject.filter(message -> message.getI
我想知道为什么 Amazon Web Services 控制台登录页面有这么长的 url?为什么不只发布数据而不显示其中包含大量数据的冗长 url。以这种方式实现有什么充分的理由吗? 最佳答案 我认为
这个问题在这里已经有了答案: Can I mix MySQL APIs in PHP? (4 个答案) 关闭 6 年前。 希望我犯了一个快速而明显的错误,我浏览了 previous question
我得到了答案:如果我禁用了cookie,那么使用URL重定向我可以传递JSESSIONID,但我的URL已经很长,因为我使用它有约束的GET方法。那怎么办我应该使用我的 session 吗?我希望我的
目前,当我使用 DOMDocument 对象并调用 saveHTML() 时,它会自动添加一些我不需要的 html 标签。我尝试了此处建议的解决方案 ( https://stackoverflow.c
我是一名优秀的程序员,十分优秀!