我有一个进度条功能。我想在另一个函数正在处理时运行此进度条。我使用多处理模块编写了一个简单的测试代码,但效果不佳。我的代码:
from multiprocessing import Process
import time
def foo(thread):
print time.ctime()
time.sleep(10)
print time.ctime()
def progress_bar(timer = 10):
digits = 4
delete = '\b' * 6
time_slot = float(timer) / 100
for i in range(1, 101):
delete_bar = '\b' * 52
if i == 1:
bar = '|' + ' ' * 50 + '|'
else:
bar = '|' + '=' * (i / 2 - 1) + '>' + ' ' * (50 - i / 2) + '|'
print "{0}{1:{2}}{3}{4}".format(delete, str(i) + '%', digits, bar, delete_bar),
time.sleep(time_slot)
print ''
def main():
p1 = Process(target = foo1('this'))
p1.start()
p2 = Process(target = progress_bar())
p2.start()
p1.join()
p2.join()
if __name__ == "__main__":
main()
我希望foo会先打印当前时间。然后progress_bar进入倒计时10秒钟。最后,foo将在最后输出另一个时间。
Tue Apr 5 11:49:47 2016
100% =================================================>|
Tue Apr 5 11:49:57 2016
然而,我从输出中得到的是这样的:
Tue Apr 5 11:49:47 2016
Tue Apr 5 11:49:57 2016
100% =================================================>|
有没有办法在Python中解决这个问题? 非常感谢你!
答案 0 :(得分:1)
这种情况正在发生,因为首先评估函数参数,并在定义p1
和p2
时实际调用foo1('this')
,它在{{1}的定义下执行函数在p1
的实例化中{}和progress_bar()
。
有关演示此内容的简单示例,请参阅以下内容:
p2
打印:
def fn():
print 'called'
return 1
target1 = fn()
target = fn
print target1
print target
我让您的示例使用下面的>>> called # Got called as soon as you called fn via fn()
>>> 1 # Assigned the return value of fn to target1
>>> <function fn at 0x12DA77F0> # Didn't get called, assigned the fn definition to target
(编辑:在查看了一些Thread
examples后,似乎他们应该使用相同的语法如下面的代码(只需更改导入并使用Process
而不是Process
),但由于某种原因,即使在复制之后我也无法获得Thread
方法进行打印例如。可能是由于我的自定义python设置,但不完全确定。):
Process
打印
from threading import Thread
import time
def foo(thread):
print(time.ctime())
time.sleep(10)
print(time.ctime())
def progress_bar(timer = 10):
digits = 4
delete = '\b' * 6
time_slot = float(timer) / 100
for i in range(1, 101):
delete_bar = '\b' * 52
if i == 1:
bar = '|' + ' ' * 50 + '|'
else:
bar = '|' + '=' * (i / 2 - 1) + '>' + ' ' * (50 - i / 2) + '|'
print("{0}{1:{2}}{3}{4}".format(delete, str(i) + '%', digits, bar, delete_bar),)
time.sleep(time_slot)
print('')
def main():
t1 = Thread(target=foo, args=('this',)) # Notice, not foo('this') <- this executes foo('this') at definition
t1.start()
t2 = Thread(target=progress_bar) # Again, notice, no parens - target is just the function definition
t2.start()
t1.join()
t2.join()
答案 1 :(得分:1)
试试这个:
from multiprocessing import Process
import time
def foo1(thread):
print time.ctime()
time.sleep(10.5)
print time.ctime()
def progress_bar(timer = 10):
digits = 4
delete = '\b' * 6
time_slot = float(timer) / 100
for i in range(1, 101):
delete_bar = '\b' * 52
if i == 1:
bar = '|' + ' ' * 50 + '|'
else:
bar = '|' + '=' * (i / 2 - 1) + '>' + ' ' * (50 - i / 2) + '|'
print "{0}{1:{2}}{3}{4}".format(delete, str(i) + '%', digits, bar, delete_bar),
time.sleep(time_slot)
print ''
def main():
p1 = Process(target=foo1, args=('this',))
# p1 = Process(target = foo1('this'))
p1.start()
time.sleep(0.1)
p2 = Process(target=progress_bar)
p2.start()
p1.join()
p2.join()
if __name__ == "__main__":
main()
注意不同的p1过程。