在本文中,我们将给您介绍关于在Python中执行定期操作的详细内容,并且为您解答python定期执行的相关问题,此外,我们还将为您提供关于php–在Python中执行多个字符串替换的最快实现、中止在P
在本文中,我们将给您介绍关于在Python中执行定期操作的详细内容,并且为您解答python 定期执行的相关问题,此外,我们还将为您提供关于php – 在Python中执行多个字符串替换的最快实现、中止在Python中执行模块、使用if语句在python中执行命令、在Python中执行php代码的知识。
本文目录一览:- 在Python中执行定期操作(python 定期执行)
- php – 在Python中执行多个字符串替换的最快实现
- 中止在Python中执行模块
- 使用if语句在python中执行命令
- 在Python中执行php代码
在Python中执行定期操作(python 定期执行)
我在Windows上工作。我想每10秒执行一次函数foo()。
我该怎么做呢?
答案1
小编典典在的末尾foo()
,创建一个在10秒后Timer调用foo()
自身的。
因为,Timer创建一个新的thread要调用foo()
。
你可以做其他事情而不会被阻止。
import time, threadingdef foo(): print(time.ctime()) threading.Timer(10, foo).start()foo()#output:#Thu Dec 22 14:46:08 2019#Thu Dec 22 14:46:18 2019#Thu Dec 22 14:46:28 2019#Thu Dec 22 14:46:38 2019
php – 在Python中执行多个字符串替换的最快实现
例如,你如何实现一个快速的函数,其行为类似于 Python中的PHP的htmlspecialchars?
我比较了(1)多个’替换’方法,(2)正则表达式方法,和(3)马特安德森的方法.
n = 10次运行,结果如下:
在100个字符上:
TIME: 0 ms [ replace_method(str) ] TIME: 5 ms [ regular_expression_method(str,dict) ] TIME: 1 ms [ matts_multi_replace_method(list,str) ]
在1000个字符上:
TIME: 0 ms [ replace_method(str) ] TIME: 3 ms [ regular_expression_method(str,dict) ] TIME: 2 ms [ matts_multi_replace_method(list,str) ]
在10000个字符上:
TIME: 3 ms [ replace_method(str) ] TIME: 7 ms [ regular_expression_method(str,dict) ] TIME: 5 ms [ matts_multi_replace_method(list,str) ]
在100000个字符:
TIME: 36 ms [ replace_method(str) ] TIME: 46 ms [ regular_expression_method(str,dict) ] TIME: 39 ms [ matts_multi_replace_method(list,str) ]
在1000000个字符上:
TIME: 318 ms [ replace_method(str) ] TIME: 360 ms [ regular_expression_method(str,dict) ] TIME: 320 ms [ matts_multi_replace_method(list,str) ]
在3687809字符:
TIME: 1.277524 sec [ replace_method(str) ] TIME: 1.290590 sec [ regular_expression_method(str,dict) ] TIME: 1.116601 sec [ matts_multi_replace_method(list,str) ]
因此,对Matt在相当大的输入字符串上击败多’替换’方法感到荣幸.
任何人都有想法在较小的字符串上击败它吗?
也许有点难以绕过下面的代码(这对我来说,我写了它),但似乎按预期运行.我没有对它进行基准测试,但我怀疑它会相当快.
def multi_replace(pairs,text): stack = list(pairs) stack.reverse() def replace(stack,parts): if not stack: return parts # copy the stack so I don't disturb parallel recursions stack = list(stack) from_,to = stack.pop() #print 'split (%r=>%r)' % (from_,to),parts split_parts = [replace(stack,part.split(from_)) for part in parts] parts = [to.join(split_subparts) for split_subparts in split_parts] #print 'join (%r=>%r)' % (from_,parts return parts return replace(stack,[text])[0] print multi_replace( [('foo','bar'),('baaz','foo'),('quux','moop')],'foobarbaazfooquuxquux')
对于:
barbarfoobarmoopmoop
中止在Python中执行模块
我想停止对正在导入的模块进行评估,而不停止整个程序。
这是我要实现的示例:
main.py
print('main1') import testmodule print('main2')
testmodule.py
print(' module1') some_condition=True if some_condition: exit_from_module() # How to do this? print(' module2') # This line is not executed.
预期产量:
main1 module1 main2
使用if语句在python中执行命令
让我看看是否了解所有用户的登录窗口都一样,对吧?然后根据用户的凭据根据其权限构建菜单窗口,假设添加特定内容或将其从菜单中删除
在Python中执行php代码
由于某些原因,我必须运行一个php脚本才能从Python获取图像。因为php脚本很大,而且不是我的,所以我需要几天的时间才能找到所使用的正确算法并将其转换为python。
我想知道是否有任何方法可以运行带有少量参数的php脚本,以返回python中的图像。
答案1
小编典典示例代码:
import subprocess# if the script don''t need output.subprocess.call("php /path/to/your/script.php")# if you want outputproc = subprocess.Popen("php /path/to/your/script.php", shell=True, stdout=subprocess.PIPE)script_response = proc.stdout.read()
关于在Python中执行定期操作和python 定期执行的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于php – 在Python中执行多个字符串替换的最快实现、中止在Python中执行模块、使用if语句在python中执行命令、在Python中执行php代码的相关知识,请在本站寻找。
本文标签: