python螺纹计时器 - 每x秒激活函数

发布于 2025-01-22 15:04:37 字数 225 浏览 4 评论 0原文

是否有任何简单的方法激活线程以每个x sec启动功能以显示一些数据?

def send_data():
    data = "Message from client"
    socket.sendall(data.encode())


write_thread = threading.Thread(target=send_data())
write_thread.start()

Is there any simple way to activate the thread to fire up the function every X sec, to display some data?

def send_data():
    data = "Message from client"
    socket.sendall(data.encode())


write_thread = threading.Thread(target=send_data())
write_thread.start()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼忆七猫命九 2025-01-29 15:04:37

您可以尝试 iSchedule 模块 - 它为计划任何给定函数提供了非常简单的语法。

这是github页面的直接示例:

from ischedule import run_loop, schedule


@schedule(interval=0.1)
def task():
    print("Performing a task")


run_loop(return_after=1)

return_after param in run_loop()是可选的超时。
另外,如果您不熟悉,@语法是Python decorator

You could try the ischedule module - it provides very straightforward syntax for scheduling any given function.

Here's an example straight from the GitHub page:

from ischedule import run_loop, schedule


@schedule(interval=0.1)
def task():
    print("Performing a task")


run_loop(return_after=1)

The return_after param in run_loop() is an optional timeout.
Also, in case you're unfamiliar, the @ syntax is a Python decorator.

不喜欢何必死缠烂打 2025-01-29 15:04:37

一个简单的方法就是这样:

import time

while True:
    task()
    time.sleep(1)

A simple way would be this:

import time

while True:
    task()
    time.sleep(1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文