Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法在Python的主线程上执行函数?_Python_Multithreading - Fatal编程技术网

有没有办法在Python的主线程上执行函数?

有没有办法在Python的主线程上执行函数?,python,multithreading,Python,Multithreading,我有以下问题。该应用程序生成多个线程(异步)。每个线程负责一个特定的任务,该任务定期执行。例如,一个线程每N秒检查一次数据库中是否有要处理的新记录。如果有,它就会开始处理(自动键入并单击屏幕)。在这个过程开始之前,我想显示一个弹出窗口来通知我,应用程序即将接管我的鼠标和键盘。问题是,显示一个弹出窗口需要我们在主线程上(我们显然不是)。 以下是片段: 在就绪功能中: threading.Timer(10, self.check_periodically).start() 在该函数中,我只有(目前

我有以下问题。该应用程序生成多个线程(异步)。每个线程负责一个特定的任务,该任务定期执行。例如,一个线程每N秒检查一次数据库中是否有要处理的新记录。如果有,它就会开始处理(自动键入并单击屏幕)。在这个过程开始之前,我想显示一个弹出窗口来通知我,应用程序即将接管我的鼠标和键盘。问题是,显示一个弹出窗口需要我们在主线程上(我们显然不是)。 以下是片段: 在就绪功能中:

threading.Timer(10, self.check_periodically).start()
在该函数中,我只有(目前):

这给了我一个错误:

2019-12-09 10:52:23.009 Python[1676:5003701] WARNING: NSWindow drag regions should only be invalidated on the Main Thread! This will throw an exception in the future. Called from (
0   AppKit                              0x0000000105e016ef -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 371
1   AppKit                              0x0000000105dfebc1 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1416
2   AppKit                              0x0000000105dfe633 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 42
3   Tk                                  0x0000000105c0f3b1 TkMacOSXMakeRealWindowExist + 766
4   Tk                                  0x0000000105c0efdc TkWmMapWindow + 56
5   Tk                                  0x0000000105b82d5d Tk_MapWindow + 155
6   Tk                                  0x0000000105b8aead TkInstallFrameMenu + 267
7   Tcl                                 0x0000000105afc4cc TclServiceIdle + 75
8   Tcl                                 0x0000000105ae3bb6 Tcl_DoOneEvent + 353
9   _tkinter.cpython-36m-darwin.so      0x0000000104ea8aa1 _tkinter_tkapp_mainloop + 209
10  Python                              0x00000001007a47b8 _PyCFunction_FastCallDict + 552
11  Python                              0x000000010082e164 call_function + 612
12  Python                              0x000000010082fb74 _PyEval_EvalFrameDefault + 5604
13  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
14  Python                              0x000000010082d500 _PyEval_EvalCodeWithName + 2720
15  Python                              0x000000010082dd2b fast_function + 219
16  Python                              0x000000010082e14b call_function + 587
17  Python                              0x000000010082fb74 _PyEval_EvalFrameDefault + 5604
18  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
19  Python                              0x000000010082d500 _PyEval_EvalCodeWithName + 2720
20  Python                              0x000000010082dd2b fast_function + 219
21  Python                              0x000000010082e14b call_function + 587
22  Python                              0x0000000100830096 _PyEval_EvalFrameDefault + 6918
23  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
24  Python                              0x000000010082d500 _PyEval_EvalCodeWithName + 2720
25  Python                              0x000000010082dd2b fast_function + 219
26  Python                              0x000000010082e14b call_function + 587
27  Python                              0x0000000100830096 _PyEval_EvalFrameDefault + 6918
28  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
29  Python                              0x000000010082e556 _PyFunction_FastCallDict + 838
30  Python                              0x0000000100748a54 _PyObject_FastCallDict + 356
31  Python                              0x0000000100749120 _PyObject_Call_Prepend + 208
32  Python                              0x0000000100748443 PyObject_Call + 99
33  Python                              0x00000001008305f6 _PyEval_EvalFrameDefault + 8294
34  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
35  Python                              0x000000010082deae fast_function + 606
36  Python                              0x000000010082e14b call_function + 587
37  Python                              0x000000010082fb74 _PyEval_EvalFrameDefault + 5604
38  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
39  Python                              0x000000010082deae fast_function + 606
40  Python                              0x000000010082e14b call_function + 587
41  Python                              0x000000010082fb74 _PyEval_EvalFrameDefault + 5604
42  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
43  Python                              0x000000010082e556 _PyFunction_FastCallDict + 838
44  Python                              0x0000000100748a54 _PyObject_FastCallDict + 356
45  Python                              0x0000000100749120 _PyObject_Call_Prepend + 208
46  Python                              0x0000000100748443 PyObject_Call + 99
47  Python                              0x00000001008305f6 _PyEval_EvalFrameDefault + 8294
48  pydevd_frame_evaluator_darwin_36_64 0x000000010485cbfb __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 2795
49  Python                              0x000000010082e556 _PyFunction_FastCallDict + 838
50  Python                              0x0000000100748a54 _PyObject_FastCallDict + 356
51  Python                              0x0000000100749120 _PyObject_Call_Prepend + 208
52  Python                              0x0000000100748443 PyObject_Call + 99
53  Python                              0x00000001007c3d83 slot_tp_call + 67
54  Python                              0x0000000100748443 PyObject_Call + 99
55  Python                              0x00000001008865e7 t_bootstrap + 71
56  libsystem_pthread.dylib             0x0000000101445d36 _pthread_start + 125
57  libsystem_pthread.dylib             0x000000010144258f thread_start + 15
)
当然,如果我在主线程上运行
pymsgbox.alert(timeout=5000)
,它就像一个符咒

有没有标准的方法来实现这一点?
来自OSX开发和Java,这是一个不需要动脑筋的问题,但我花了这么多时间调试它,以至于我的编程技能在这里受到质疑…:)

也许波斯特可以帮你。我打赌是用那篇帖子所做的
Event
@Kendoka,事实上,帮了我的忙,谢谢你。我会再等几天,如果没有人发布干净的解决方案,我会自己发布。这有点骇人,但对我来说已经足够了。也许post可以帮你。我打赌是用那篇帖子所做的
Event
@Kendoka,事实上,帮了我的忙,谢谢你。我会再等几天,如果没有人发布干净的解决方案,我会自己发布。这有点粗俗,但对我来说已经足够了。
2019-12-09 10:52:23.009 Python[1676:5003701] WARNING: NSWindow drag regions should only be invalidated on the Main Thread! This will throw an exception in the future. Called from (
0   AppKit                              0x0000000105e016ef -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 371
1   AppKit                              0x0000000105dfebc1 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1416
2   AppKit                              0x0000000105dfe633 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 42
3   Tk                                  0x0000000105c0f3b1 TkMacOSXMakeRealWindowExist + 766
4   Tk                                  0x0000000105c0efdc TkWmMapWindow + 56
5   Tk                                  0x0000000105b82d5d Tk_MapWindow + 155
6   Tk                                  0x0000000105b8aead TkInstallFrameMenu + 267
7   Tcl                                 0x0000000105afc4cc TclServiceIdle + 75
8   Tcl                                 0x0000000105ae3bb6 Tcl_DoOneEvent + 353
9   _tkinter.cpython-36m-darwin.so      0x0000000104ea8aa1 _tkinter_tkapp_mainloop + 209
10  Python                              0x00000001007a47b8 _PyCFunction_FastCallDict + 552
11  Python                              0x000000010082e164 call_function + 612
12  Python                              0x000000010082fb74 _PyEval_EvalFrameDefault + 5604
13  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
14  Python                              0x000000010082d500 _PyEval_EvalCodeWithName + 2720
15  Python                              0x000000010082dd2b fast_function + 219
16  Python                              0x000000010082e14b call_function + 587
17  Python                              0x000000010082fb74 _PyEval_EvalFrameDefault + 5604
18  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
19  Python                              0x000000010082d500 _PyEval_EvalCodeWithName + 2720
20  Python                              0x000000010082dd2b fast_function + 219
21  Python                              0x000000010082e14b call_function + 587
22  Python                              0x0000000100830096 _PyEval_EvalFrameDefault + 6918
23  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
24  Python                              0x000000010082d500 _PyEval_EvalCodeWithName + 2720
25  Python                              0x000000010082dd2b fast_function + 219
26  Python                              0x000000010082e14b call_function + 587
27  Python                              0x0000000100830096 _PyEval_EvalFrameDefault + 6918
28  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
29  Python                              0x000000010082e556 _PyFunction_FastCallDict + 838
30  Python                              0x0000000100748a54 _PyObject_FastCallDict + 356
31  Python                              0x0000000100749120 _PyObject_Call_Prepend + 208
32  Python                              0x0000000100748443 PyObject_Call + 99
33  Python                              0x00000001008305f6 _PyEval_EvalFrameDefault + 8294
34  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
35  Python                              0x000000010082deae fast_function + 606
36  Python                              0x000000010082e14b call_function + 587
37  Python                              0x000000010082fb74 _PyEval_EvalFrameDefault + 5604
38  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
39  Python                              0x000000010082deae fast_function + 606
40  Python                              0x000000010082e14b call_function + 587
41  Python                              0x000000010082fb74 _PyEval_EvalFrameDefault + 5604
42  pydevd_frame_evaluator_darwin_36_64 0x000000010485dba6 __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 6806
43  Python                              0x000000010082e556 _PyFunction_FastCallDict + 838
44  Python                              0x0000000100748a54 _PyObject_FastCallDict + 356
45  Python                              0x0000000100749120 _PyObject_Call_Prepend + 208
46  Python                              0x0000000100748443 PyObject_Call + 99
47  Python                              0x00000001008305f6 _PyEval_EvalFrameDefault + 8294
48  pydevd_frame_evaluator_darwin_36_64 0x000000010485cbfb __pyx_f_18_pydevd_frame_eval_35pydevd_frame_evaluator_darwin_36_64_get_bytecode_while_frame_eval + 2795
49  Python                              0x000000010082e556 _PyFunction_FastCallDict + 838
50  Python                              0x0000000100748a54 _PyObject_FastCallDict + 356
51  Python                              0x0000000100749120 _PyObject_Call_Prepend + 208
52  Python                              0x0000000100748443 PyObject_Call + 99
53  Python                              0x00000001007c3d83 slot_tp_call + 67
54  Python                              0x0000000100748443 PyObject_Call + 99
55  Python                              0x00000001008865e7 t_bootstrap + 71
56  libsystem_pthread.dylib             0x0000000101445d36 _pthread_start + 125
57  libsystem_pthread.dylib             0x000000010144258f thread_start + 15
)