How to use twisted module for messaging

80 views
Skip to first unread message

PBLN RAO

unread,
Dec 10, 2012, 8:12:45 AM12/10/12
to python_in...@googlegroups.com
Hi,

i have an application developed which works some parts internal to maya and some work as external to maya.

We need to develop a communication between system [i.e., chatting or messaging what ever you say :)]

I have a taught in mind of using twisted module for it. can anyone suggest where and how i should start....?

the messaging should work in asynchronous mode so that my application will not hang when its in listen mode for receiving messages.

Justin Israel

unread,
Dec 10, 2012, 1:52:12 PM12/10/12
to python_in...@googlegroups.com
Did you check out the docs and tutorials for twisted yet? Or are you asking if twisted is a good solution, or if something else can be recommended?
What kind of communication will it be doing? 



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To post to this group, send email to python_in...@googlegroups.com.
To unsubscribe from this group, send email to python_inside_m...@googlegroups.com.



PBLN RAO

unread,
Dec 10, 2012, 11:26:00 PM12/10/12
to python_in...@googlegroups.com
Actually i tiered sockets and developed the TCP_Messaging. its working but when a message is received the application hangs. I made listening of messaging to run in a thread and the data (message) is received it calls a windows pops up at screen bottom right. As this all is happening in thread the application hangs after receiving the 1st message.

I read that twisted is a robust solution for this type of messaging.

Main agenda for developing this is for a two side communication between the employees for are using this application installed.

For example:

I have a review system, where artists submits playblasts through this app and details are saved in a database, supervisor will also use this app to review the submitted files. so when he starts review, app will be sending messages to the artist for a call to attend the review.

Hope i made you clear about the main purpose of communication.

Justin Israel

unread,
Dec 11, 2012, 1:41:10 AM12/11/12
to python_in...@googlegroups.com
Can you show some code surrounding the receiving of messages where you say it hangs the app? 

PBLN RAO

unread,
Dec 11, 2012, 3:51:57 AM12/11/12
to python_in...@googlegroups.com
Attached the TCP_Messaging module

Usage on system 1:

from TCP_Chat_04 import TCP_Messaging

myChat = TCP_Messaging()
myChat.Connect()

Usage on system 2:

from TCP_Chat_04 import TCP_Messaging

myChat = TCP_Messaging()
myChat.SendData('login_audio_Test message to display')

Note:
My message popup window has a method "DisplayMessage" to display the message. the string format should be (login_audiofile_Message)

let me know if any more details are required...
TCP_Chat_04.py

Justin Israel

unread,
Dec 11, 2012, 11:57:19 AM12/11/12
to python_in...@googlegroups.com
You have the right idea about what is causing the hang.
Whether you continue to use python sockets, or switch to using twisted, you will be faced with the same situation. Creating QWidgets and calling their method must be done in the main GUI thread. Other threads doing work need to communicate using signals/slots, which will queue up operations into the main thread for execution.

Your Listen thread starts running and waiting for connections. When a connection happens from a client, the server accepts it and runs your MessageHandler for the request. This is still in a thread. I am assuming messageWindow() is creating a QWidget instance, and then DisplayMessage(msg) is trying to perform GUI operations directly on that object.

What you will want to do instead is emit signals when a message is received. Connect to this signal in your main gui thread with a slot that can receive the message and interact with your QWidget that you want to show.

The python SocketServer, I think, makes it a little more complicated because the handler is a separate object from the server, and your server is hidden away as a member of the thread. The first question is really whether you need every connection to be a separate thread because they do a bunch of work or not. If all they are doing is connecting, sending some text, and disconnecting, it can really just be a server running in a thread which accepts messages synchronously.

Anyways, here is an example that just tweaks what you already have:

We make your Listen thread -> QThread to gain the ability to emit signals and still be a thread all in one.
We make your TCP_Messaging class -> QObject to also emit signals.
The idea being that the handler reads the message and emits a messageReceived signal. We have to be a little hacky because the handler only knows about the server and the server is not a a QObject, so really what I did was just copy the signal reference onto it so the handler can see it. What you could do is make your own SocketServer.TCPServer subclass that also inherits QObject, so that it can be the one to own the messageReceived signal. But thats up to you since this is just an example.

Then we expose another messageReceived signal to the world, on your TCP_Messaging class. All this does is connect from signal->signal on the server that is created when you call Connect(). The effect is that when a message is emitted from a handler, it goes to the server which is then forwarded out to the world.

So the usage is that you just start the server and connect a main thread slot to it for receiving messages.

A more compact and cleaner way of doing this same thing would be to just make your own Server class QObject which runs a loop of accepting clients and handling them, all in one place. This would make it much easier to just emit a signal from one public spot.

-- justin



On Dec 11, 2012, at 12:51 AM, PBLN RAO wrote:

<TCP_Chat_04.py>

PBLN RAO

unread,
Dec 12, 2012, 3:08:48 AM12/12/12
to python_in...@googlegroups.com
Thx justin,
You really helped me...

on the other hand i am developing this messaging system on QtNetwork also.

Justin Israel

unread,
Dec 12, 2012, 9:10:44 AM12/12/12
to python_in...@googlegroups.com
Well there ya go! If you have the QtNetwork module then you can use TCPServer!

PBLNRAO Kiran

unread,
Dec 27, 2012, 11:39:42 PM12/27/12
to python_in...@googlegroups.com

Justin when i run you modified code of mine i dont receive and msg. instead i get below error

Messenger service is not running at target system.
configure server socket

Justin Israel

unread,
Dec 28, 2012, 2:03:11 AM12/28/12
to python_in...@googlegroups.com
Check your address and port. 
Reply all
Reply to author
Forward
0 new messages