Search code examples
pythonpython-2.7socketspygtkgtk2

How to listen socket, when app is running in gtk.main()?


I've tried to search, but nothing seems to work. For example:

Separate threads

I am currently writing simple communication app in Python2.7 with PyGTK. I have background process, which is listening socket in while loop like that. I want to display message, when I receive it form server. But when I start gtk.main(), it obviously starts to loop again, so my listening loop doesn't working. I need to display new window for each message or close old window and display new window with all unread messages. Can I somehow display window (or more than one window) and don't block my listening loop?

IOChannel solution

 while True: #listening loop
  print 'Start'
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
  sock.bind((socket.gethostname(), 80))
  sock.listen(2)
  conn, address = sock.accept()
  conn.sendall('200 OK')
  fd = conn.fileno() #Make file descriptor from socket
  gio_channel = glib.IOChannel(fd) #Pass it to IOChannel

  if(new_message):
    #display message with gtk.main()
  else
    #Do something else like update file

subprocess.Popen solution

while True: #listening loop
  print 'Start'
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
  sock.bind((socket.gethostname(), 80))
  sock.listen(2)
  conn, address = sock.accept()
  conn.sendall('200 OK')
  all_json = conn.recv(2048)

  if(new_message):
    # This script run pygtk app in the background
    subprocess.Popen(['python', 'communication_app.py'])
  else
    #Do something else like update file

Solution

  • You may want to give a try using GLib's GIOChannel (GLib.IOChannel in the Python doc) to handle the sockets as en event source (a GSource). Then use g_io_create_watch (GLib.IOChannel.add_watch in Python) to integrate in GTK+ main loop.

    You also have lots of more advanced socket handling stuff in the Gio library: