鱼C论坛

 找回密码
 立即注册
查看: 458|回复: 7

[已解决]micropython代码求助

[复制链接]
发表于 2024-1-2 08:59:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
@fishc

写一个micro python代码,多线程,一个线程不停输出“hello micro”,只有在另一个线程输入“python”时停止。
最佳答案
2024-1-2 08:59:44
好的,下面是一个使用MicroPython多线程实现的代码示例:

  1. import _thread
  2. import time

  3. def output_thread():
  4.     while True:
  5.         print("hello micro")
  6.         time.sleep(1)

  7. def input_thread():
  8.     while True:
  9.         user_input = input()
  10.         if user_input == "python":
  11.             _thread.exit()

  12. _thread.start_new_thread(output_thread, ())
  13. _thread.start_new_thread(input_thread, ())

  14. while True:
  15.     pass
复制代码


在这个代码中,我们定义了两个线程:`output_thread`和`input_thread`。`output_thread`函数会不停地输出"hello micro",而`input_thread`函数会等待用户输入,如果用户输入的是"python",则调用`_thread.exit()`退出该线程。

最后,我们通过`_thread.start_new_thread()`函数分别启动这两个线程,并通过一个死循环来保持程序的运行,直到`input_thread`线程退出。

希望对你有帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-1-2 08:59:44 | 显示全部楼层    本楼为最佳答案   
好的,下面是一个使用MicroPython多线程实现的代码示例:

  1. import _thread
  2. import time

  3. def output_thread():
  4.     while True:
  5.         print("hello micro")
  6.         time.sleep(1)

  7. def input_thread():
  8.     while True:
  9.         user_input = input()
  10.         if user_input == "python":
  11.             _thread.exit()

  12. _thread.start_new_thread(output_thread, ())
  13. _thread.start_new_thread(input_thread, ())

  14. while True:
  15.     pass
复制代码


在这个代码中,我们定义了两个线程:`output_thread`和`input_thread`。`output_thread`函数会不停地输出"hello micro",而`input_thread`函数会等待用户输入,如果用户输入的是"python",则调用`_thread.exit()`退出该线程。

最后,我们通过`_thread.start_new_thread()`函数分别启动这两个线程,并通过一个死循环来保持程序的运行,直到`input_thread`线程退出。

希望对你有帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-1-2 09:01:15 | 显示全部楼层
FishC 发表于 2024-1-2 08:59
好的,下面是一个使用MicroPython多线程实现的代码示例:

检查代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-1-2 09:01:46 | 显示全部楼层


非常抱歉,MicroPython中的线程模块名字并不是`_thread`,而是`_thread`。除此之外,在MicroPython中,`print`函数并不支持直接输出到终端,需要使用`uasyncio`模块来实现非阻塞的打印输出。下面是经过修正后的代码示例:

  1. import uasyncio as asyncio

  2. async def output_task():
  3.     while True:
  4.         print("hello micro")
  5.         await asyncio.sleep(1)

  6. async def input_task():
  7.     while True:
  8.         user_input = await asyncio.get_event_loop().run_in_executor(None, input)
  9.         if user_input == "python":
  10.             break

  11. async def main():
  12.     output = asyncio.create_task(output_task())
  13.     input = asyncio.create_task(input_task())
  14.     await asyncio.gather(output, input)

  15. loop = asyncio.get_event_loop()
  16. loop.run_until_complete(main())
复制代码


在这个示例中,我们使用了`uasyncio`模块来管理异步任务。首先,我们定义了两个协程任务:`output_task`用于输出"hello micro",`input_task`用于等待用户输入并判断是否为"python"。然后,我们在`main`函数中创建了这两个任务,并使用`asyncio.gather`函数同时运行它们。最后,通过调用`loop.run_until_complete`来运行整个协程程序。

希望这次修改后的代码能够满足你的需求!请注意在MicroPython环境中运行代码时可能需要适配对应板子和软件库。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-1-2 09:16:57 | 显示全部楼层
FishC 发表于 2024-1-2 08:59
好的,下面是一个使用MicroPython多线程实现的代码示例:

不能停止输出
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-1-2 09:17:10 | 显示全部楼层


对不起,我之前的回答有误。MicroPython中的多线程并不能在没有特殊处理的情况下安全停止线程。然而,我们可以使用`Event`对象来实现线程间的通信和控制。下面是修改后的代码示例:

  1. import _thread
  2. import time

  3. # 创建一个 Event 对象作为线程间的通信标志
  4. stop_event = _thread.Event()

  5. def output_thread():
  6.     while True:
  7.         if stop_event.is_set():
  8.             break
  9.         print("hello micro")
  10.         time.sleep(1)

  11. def input_thread():
  12.     while True:
  13.         user_input = input()
  14.         if user_input == "python":
  15.             stop_event.set()
  16.             break

  17. _thread.start_new_thread(output_thread, ())
  18. _thread.start_new_thread(input_thread, ())

  19. while True:
  20.     pass
复制代码


在这个代码中,我们引入了 `Event` 对象 `stop_event`,用于在线程间发送信号。`output_thread` 线程会不断检查 `stop_event` 是否被设置,如果设置了则退出循环。而 `input_thread` 线程在接收到用户输入 "python" 后调用 `stop_event.set()` 设置 `stop_event`,从而通知 `output_thread` 停止输出,然后退出循环。

这样,当用户输入 "python" 时,`output_thread` 将会停止输出,并结束执行。希望这次能满足你的需求!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-1-2 09:21:55 | 显示全部楼层
FishC 发表于 2024-1-2 09:17
对不起,我之前的回答有误。MicroPython中的多线程并不能在没有特殊处理的情况下安全停止线程。然而, ...

AttributeError: 'module' object has no attribute 'Event'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-1-2 09:22:02 | 显示全部楼层
yc0526vip 发表于 2024-01-02 09:21
AttributeError: 'module' object has no attribute ' ...


对不起,MicroPython中没有内置的`Event`类。但是我们可以通过其他方式实现线程间的通信和控制。下面是一个修正后的代码示例,使用共享变量来实现线程的停止控制:

  1. import _thread
  2. import time

  3. # 创建一个共享变量作为线程间的通信标志
  4. stop_flag = False

  5. def output_thread():
  6.     while not stop_flag:
  7.         print("hello micro")
  8.         time.sleep(1)

  9. def input_thread():
  10.     global stop_flag
  11.     while True:
  12.         user_input = input()
  13.         if user_input == "python":
  14.             stop_flag = True
  15.             break

  16. _thread.start_new_thread(output_thread, ())
  17. _thread.start_new_thread(input_thread, ())
  18. while True:
  19.     pass
复制代码


在这个修正后的代码中,我们使用了一个全局变量 `stop_flag` 作为线程间的通信标志。`output_thread` 线程在每次循环中检查 `stop_flag` 的值,如果为 `True` 则停止输出。`input_thread` 线程在接收到用户输入 "python" 后将 `stop_flag` 置为 `True`,从而通知 `output_thread` 停止输出。这样,当用户输入 "python" 时,`output_thread` 将会停止输出,并结束执行。

再次抱歉之前的错误,请尝试这个修正后的代码。希望能帮到你!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 16:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表