发布时间: 2026-01-19

1.2.5 线程使用


脚本语言支持用户创建线程的功能,用户可以通过创建线程实现复杂的机器人控制逻辑。

1. 创建和关闭线程的方法

python 1 # 定义线程函数,定义方式与普通函数相同 2 def thread_add_func(a,b): 3 c=a + b 4 print(c) 5 return c 6 end 7 8 9 # 运行线程函数,此线程函数会被异步执行。 start_thread 函数的返回值为线程句柄,可以 10 # 在停止线程运行时使用。 11 thread_handler=start_thread(thread_add_func,(1,3)) 12 13 # 停止线程函数运行,stop _thread 函数的参数值为运行线程时返回的句柄。 14 stop _thread(thread_handler)

2. 使用线程功能时的注意事项

  • 任务停止运行后,所有创建的线程都将自动结束运行;
  • 启动线程函数时,可以传递参数;
  • 子线程或主线程内的循环语句, 请严格避免无耗时操作的死循环, 此操作可能影响系统整体性能。耗时操作主要为sleep()、sync()、移动指令等。
提交反馈