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

眉间皱痕 提交于 2019-12-11 08:29:32

问题


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

回答1:


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.

  • https://lazka.github.io/pgi-docs/#GLib-2.0/classes/IOChannel.html#GLib.IOChannel
  • https://developer.gnome.org/glib/stable/glib-IO-Channels.html

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

  • https://lazka.github.io/pgi-docs/#Gio-2.0
  • https://developer.gnome.org/gio/stable/ (see "Low-level network support" and "High-level network functionallity" sections)


来源:https://stackoverflow.com/questions/47265105/how-to-listen-socket-when-app-is-running-in-gtk-main

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!