Python子进程的通用实践
不幸的是,我仅限于Python2.4,我希望在脚本执行时运行ascii动画(即旋转循环),我只是想知道这样做的常用方法或实践是什么,以及与解决方案相关的任何/所有资源,示例脚本将非常棒!!我一直在使用os.sytem('command'),希望改掉这个习惯Python子进程的通用实践,python,python-2.4,Python,Python 2.4,不幸的是,我仅限于Python2.4,我希望在脚本执行时运行ascii动画(即旋转循环),我只是想知道这样做的常用方法或实践是什么,以及与解决方案相关的任何/所有资源,示例脚本将非常棒!!我一直在使用os.sytem('command'),希望改掉这个习惯 谢谢 一种可能的方法是使用回车符“\r”将光标返回到行首,以便覆盖以前写入的字符。这允许您创建动画,只要它适合当前行。例如: import time def do_a_little_work(): time.sleep(0.1)
谢谢 一种可能的方法是使用回车符“\r”将光标返回到行首,以便覆盖以前写入的字符。这允许您创建动画,只要它适合当前行。例如:
import time
def do_a_little_work():
time.sleep(0.1)
print "about to do work..."
icons = ["-", "/", "|", "\\"]
icon_idx = 0
while True:
do_a_little_work()
#todo: check if work is done, and break out of the loop.
print "\r" + icons[icon_idx],
icon_idx = (icon_idx+1)%len(icons)
print "\rdone."
结果:
about to do work...
-
变成
about to do work...
/
about to do work...
|
about to do work...
\
变成
about to do work...
/
about to do work...
|
about to do work...
\
变成
about to do work...
/
about to do work...
|
about to do work...
\
等等。。。终于成为
about to do work...
done.
您可以使用
threading
与常规代码同时运行动画
from threading import Thread
import time
def do_the_work():
#execute your script here
work_thread = Thread(target=do_the_work)
print "Working..."
work_thread.start()
icons = ["-", "/", "|", "\\"]
icon_idx = 0
while work_thread.is_alive():
time.sleep(0.1)
print "\r" + icons[icon_idx],
icon_idx = (icon_idx+1)%len(icons)
print "\rdone"
一种可能的方法是使用回车符“\r”将光标返回到行首,以便覆盖以前写入的字符。这允许您创建动画,只要它适合当前行。例如:
import time
def do_a_little_work():
time.sleep(0.1)
print "about to do work..."
icons = ["-", "/", "|", "\\"]
icon_idx = 0
while True:
do_a_little_work()
#todo: check if work is done, and break out of the loop.
print "\r" + icons[icon_idx],
icon_idx = (icon_idx+1)%len(icons)
print "\rdone."
结果:
about to do work...
-
变成
about to do work...
/
about to do work...
|
about to do work...
\
变成
about to do work...
/
about to do work...
|
about to do work...
\
变成
about to do work...
/
about to do work...
|
about to do work...
\
等等。。。终于成为
about to do work...
done.
您可以使用
threading
与常规代码同时运行动画
from threading import Thread
import time
def do_the_work():
#execute your script here
work_thread = Thread(target=do_the_work)
print "Working..."
work_thread.start()
icons = ["-", "/", "|", "\\"]
icon_idx = 0
while work_thread.is_alive():
time.sleep(0.1)
print "\r" + icons[icon_idx],
icon_idx = (icon_idx+1)%len(icons)
print "\rdone"
一种可能的方法是使用回车符“\r”将光标返回到行首,以便覆盖以前写入的字符。这允许您创建动画,只要它适合当前行。例如:
import time
def do_a_little_work():
time.sleep(0.1)
print "about to do work..."
icons = ["-", "/", "|", "\\"]
icon_idx = 0
while True:
do_a_little_work()
#todo: check if work is done, and break out of the loop.
print "\r" + icons[icon_idx],
icon_idx = (icon_idx+1)%len(icons)
print "\rdone."
结果:
about to do work...
-
变成
about to do work...
/
about to do work...
|
about to do work...
\
变成
about to do work...
/
about to do work...
|
about to do work...
\
变成
about to do work...
/
about to do work...
|
about to do work...
\
等等。。。终于成为
about to do work...
done.
您可以使用
threading
与常规代码同时运行动画
from threading import Thread
import time
def do_the_work():
#execute your script here
work_thread = Thread(target=do_the_work)
print "Working..."
work_thread.start()
icons = ["-", "/", "|", "\\"]
icon_idx = 0
while work_thread.is_alive():
time.sleep(0.1)
print "\r" + icons[icon_idx],
icon_idx = (icon_idx+1)%len(icons)
print "\rdone"
一种可能的方法是使用回车符“\r”将光标返回到行首,以便覆盖以前写入的字符。这允许您创建动画,只要它适合当前行。例如:
import time
def do_a_little_work():
time.sleep(0.1)
print "about to do work..."
icons = ["-", "/", "|", "\\"]
icon_idx = 0
while True:
do_a_little_work()
#todo: check if work is done, and break out of the loop.
print "\r" + icons[icon_idx],
icon_idx = (icon_idx+1)%len(icons)
print "\rdone."
结果:
about to do work...
-
变成
about to do work...
/
about to do work...
|
about to do work...
\
变成
about to do work...
/
about to do work...
|
about to do work...
\
变成
about to do work...
/
about to do work...
|
about to do work...
\
等等。。。终于成为
about to do work...
done.
您可以使用
threading
与常规代码同时运行动画
from threading import Thread
import time
def do_the_work():
#execute your script here
work_thread = Thread(target=do_the_work)
print "Working..."
work_thread.start()
icons = ["-", "/", "|", "\\"]
icon_idx = 0
while work_thread.is_alive():
time.sleep(0.1)
print "\r" + icons[icon_idx],
icon_idx = (icon_idx+1)%len(icons)
print "\rdone"
你有可用的
诅咒吗?它可以让你在终端上做一些有趣的事情(尽管你应该记住库的名字不是随机的…你在学习/使用它的时候会说很多咒语)。你有可用的咒语吗?它可以让你在终端上做一些有趣的事情(尽管你应该记住库的名字不是随机的…你在学习/使用它的时候会说很多咒语)。你有可用的咒语吗?它可以让你在终端上做一些有趣的事情(尽管你应该记住库的名字不是随机的…你在学习/使用它的时候会说很多咒语)。你有可用的咒语吗?它可以让你在终端上做一些有趣的事情(尽管你应该记住,库的名称不是随机的…你在学习/使用它时会说很多脏话)。谢谢你的回复,我真的很想让它和我的其他脚本同时运行,不是关于动画本身,你可以使用线程来实现这一点。编辑。谢谢你的回复,我真的很想知道这个脚本是否能和我的其他脚本同时运行,而不是自己制作动画。你可以使用线程。编辑。谢谢你的回复,我真的很想知道这个脚本是否能和我的其他脚本同时运行,而不是自己制作动画。你可以使用线程。编辑。谢谢你的回复,我真的很想知道这个脚本是否能和我的其他脚本同时运行,而不是自己制作动画。你可以使用线程。编辑。