views:

560

answers:

2

Hello. Is there any build-in syntax in python that allows me to post a message to specific python thread inside my problem? Like 'queued connected signal' in pyQt or ::PostMessage() in Windows. I need this for asynchronous communication between program parts: there is a number of threads that handle network events and they need to post these events to a single 'logic' thread that translates events safe single-threaded way.

+1  A: 

I'm not really sure what you are looking for. But there is certainly no built-in syntax for that. Have a look at the queue and threading modules. There is a lot of helpful stuff like Queues, Conditions, Events, Locks and Semaphores that can be used to implement all kind of synchronous and asynchronous communications.

unbeknown
i'm looking for an easy way to call a function in one thread and linked function will be called in another thread ( queued delegate ). Synchronization primitives will force to do it all 'by hand' that is a lot of code?
Eye of Hell
@Eye of Hell: Please actually read the queue module documentation. What appears to be "calling a function" between threads is usually a queue of requests passed from one thread to another; the receiving thread dequeues the request and calls the function.
S.Lott
ah, i remember, function is first-class object in python? ( build-in delegates ). Is it any weel-known syntax to put a 'function call' into queue and do actual call on dequeue in actual thread? And what about function arguments marshalling?
Eye of Hell
+4  A: 

The Queue module is python is well suited to what you're describing.

You could have one queue set up that is shared between all your threads. The threads that handle the network events can use queue.put to post events onto the queue. The logic thread would use queue.get to retrieve events from the queue.

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)
Chris AtLee
Thanks! is it any way to put a function call inside an event? Actual threads code call functions, like PostConnectionStatus( STATUS ), and worker thread has handlers like OnConnectionStatus( i_status ). Is it any way to marshall function calls automatically with an event ?
Eye of Hell
In Python, functions are objects like everything else, and so can be passed around just like other objects. So you could also attach a function to be called with the event, likeq.put((e, PostConnectionStatus)). Your logic thread could then do "e, func = q.get()".Does this help?
Chris AtLee
sure thing, thanks. Variable number of arguments can be marshalled same way?
Eye of Hell
Yes. You can put any python object you want onto the queue, so a tuple or list containing your event, function and arguments could be added, or a dictionary, or a class that you've written.
Chris AtLee