I have a GUI thread and Main thread. After closing a window I have method called inside the GUI thread. I would like to propagate this to Main thread to end its work. Main thread is doing several steps, so I am able to set stop_event, but I do not want to check after each line of code for Main thread if stop_event is set.

我有一个GUI线程和主线程。关闭窗口后,我在GUI线程中调用了一个方法。我想将此传播到主线程以结束其工作。主线程正在执行几个步骤,因此我可以设置stop_event,但是如果设置了stop_event,我不想在主线程的每行代码后检查。

Thank you for your advices.

谢谢你的建议。

1 个解决方案

#1


0

If your purpose is just to terminate main thread from the child thread, try the below.

如果您的目的只是从子线程终止主线程,请尝试以下操作。

import threading
import signal
import time
import os


def main():
    threading.Thread(target=child).start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt as e:
        # KeyboardInterrupt happens by `signal.SIGINT` from the child thread.
        print('Main thread handle something before it exits')
    print('End main')

def child():
    print('Run child')
    time.sleep(2)
    # Send a signal `signal.SIGINT` to main thread.
    # The signal only head for main thread.
    os.kill(os.getpid(), signal.SIGINT)
    print('End child')


if __name__ == '__main__':
    main()

更多相关文章

  1. Python学习笔记18:Python多线程编程
  2. python 虚拟机是单线程;当线程执行I/O密集型操作是,ce
  3. Python随心记--进程、线程
  4. python多线程文件传输范例(C/S)
  5. 【好文收藏】理解python多线程
  6. Linux--多线程之线程的取消pthread_cancel
  7. Linux多线程——异步
  8. linux c++ 多线程心得
  9. [置顶] 每天进步一点点——Linux中的线程局部存储(一)

随机推荐

  1. 【Android(安卓)framework】AndroidManag
  2. android:allowClearUserData
  3. 安卓自定义属性,Android(安卓)studio无法
  4. 界面有Edittext时有些手机进入界面会自动
  5. android之ListView的item布局问题总结
  6. android工程导出成为jar包。
  7. Android实现ListView异步加载图片
  8. Java,Android Integer和byte的相互转换,Jav
  9. 用ndk-build 工具手动生成编译android原
  10. Android 自定义View(API解析+Demo)