简体   繁体   English

python如何循环一个函数等待而不暂停整个程序

[英]python how to loop a function with wait without pausing the whole program

Many people here tell you to use threading but how do you have the rest of the program running while that thread sleeps, and reruns, and sleeps again.. etc. I have tried normal threading with things like a while loop but that didn't work for me 这里有很多人告诉你使用线程,但是当线程休眠,重新运行,再次睡觉时,你如何运行程序的其余部分......等等我尝试过像while循环这样的常规线程,但是没有为我工作

edit: so the question is: how do you sleep a thread without pausing the whole program in python, if possible could you give me a example of how to do it? 编辑:所以问题是:你如何在没有暂停python中的整个程序的情况下睡眠线程,如果可能,你能给我一个如何做的例子吗?

edit 2: and if possible without tkinter 编辑2:如果可能的话没有tkinter

edit 3: fixed it, it already worked but i didn't see it because printing doesn't work across threads... Silly me. 编辑3:修复它,它已经工作但我没有看到它因为打印不能跨线程...傻我。

I think, you must look for asyncio library in python, which has capabilities for pausing and resuming your function and make your function as state machine. 我想,你必须在python中寻找asyncio库,它具有暂停和恢复你的功能并使你的功能成为状态机的功能。 For your sleep requirement, there's asyncio.sleep function available. 对于您的睡眠要求,可以使用asyncio.sleep功能。 Refer to http://www.programcreek.com/python/example/81223/asyncio.sleep to see examples of asyncio.sleep. 请参阅http://www.programcreek.com/python/example/81223/asyncio.sleep以查看asyncio.sleep的示例。

Also refer to good article at https://hackernoon.com/asyncio-for-the-working-python-developer-5c468e6e2e8e#.frn3lo7vb 另请参阅https://hackernoon.com/asyncio-for-the-working-python-developer-5c468e6e2e8e#.frn3lo7vb上的好文章

Silly me, threading works but printing doesn't work across threads. 愚蠢的我,线程工作,但打印不能跨线程。 Thanks for all the help! 感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM