Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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_Events_Delegates - Fatal编程技术网

python中是否有内置的跨线程事件?

python中是否有内置的跨线程事件?,python,events,delegates,Python,Events,Delegates,python中是否有任何内置语法允许我将消息发布到问题中的特定python线程?类似于pyQt中的“排队连接信号”或Windows中的::PostMessage()。对于程序部分之间的异步通信,我需要这样做:有许多线程处理网络事件,它们需要将这些事件发布到一个“逻辑”线程,该线程以安全的单线程方式转换事件。我不太确定您要找的是什么。但这肯定没有内置的语法。请查看和模块。有很多有用的东西,比如队列、条件、事件、锁和信号量,可以用来实现各种同步和异步通信。模块是python,非常适合您所描述的内容

python中是否有任何内置语法允许我将消息发布到问题中的特定python线程?类似于pyQt中的“排队连接信号”或Windows中的::PostMessage()。对于程序部分之间的异步通信,我需要这样做:有许多线程处理网络事件,它们需要将这些事件发布到一个“逻辑”线程,该线程以安全的单线程方式转换事件。

我不太确定您要找的是什么。但这肯定没有内置的语法。请查看和模块。有很多有用的东西,比如队列、条件、事件、锁和信号量,可以用来实现各种同步和异步通信。

模块是python,非常适合您所描述的内容

您可以设置一个在所有线程之间共享的队列。处理网络事件的线程可以使用queue.put将事件发布到队列中。逻辑线程将使用queue.get从队列中检索事件

import Queue
# maxsize of 0 means that we can put an unlimited number of events
# on the queue
q = Queue.Queue(maxsize=0)

def network_thread():
    while True:
        e = get_network_event()
        q.put(e)

def logic_thread():
    while True:
        # This will wait until there are events to process
        e = q.get()
        process_event(e)

我正在寻找一种在一个线程中调用函数的简单方法,链接函数将在另一个线程(排队委托)中调用。同步原语将强制“手动”完成这一切,这需要大量代码?@地狱之眼:请实际阅读队列模块文档。线程之间的“调用函数”通常是从一个线程传递到另一个线程的请求队列;接收线程将请求排出队列并调用函数。啊,我记得,函数是python中的第一类对象吗?(内置代理)。将“函数调用”放入队列中,并在实际线程中对出列执行实际调用,这是否有一些已知的语法?那么函数参数编组呢?谢谢!有没有办法将函数调用放入事件中?实际线程代码调用函数,如PostConnectionStatus(状态),工作线程具有OnConnectionStatus(i_状态)等处理程序。在Python中,函数和其他对象一样都是对象,因此可以像其他对象一样进行传递。因此,您还可以附加一个要随事件调用的函数,如q.put((e,PostConnectionStatus))。然后,逻辑线程可以执行“e,func=q.get()”。这有用吗?当然,谢谢。可变数量的参数可以以相同的方式封送?是。您可以将任何想要的python对象放到队列中,这样就可以添加一个包含事件、函数和参数的元组或列表,或者一个字典,或者一个您编写的类。