import gi gi.require_version('Gtk', '3.0') from gi.repository import GObject def handle_file_read(source, condition): if condition == GObject.IOCondition.IN: data = source.readline() print(data) return True else: print("Error") return False channel = GObject.IOChannel() channel = channel.open("file.txt", "r") channel.add_watch(GObject.IOCondition.IN, handle_file_read)
import gi gi.require_version('Gtk', '3.0') from gi.repository import GObject def handle_socket_read(source, condition): if condition == GObject.IOCondition.IN: data = source.recv(1024) print(data) return True else: print("Error") return False channel = GObject.IOChannel() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(("localhost", 8000)) channel = channel.unix_new(sock.fileno()) channel.add_watch(GObject.IOCondition.IN, handle_socket_read)In this example, we create a socket connection to a server running on localhost and monitor it for incoming data using the io_add_watch() method. The handle_socket_read function is called whenever data is available to read from the socket connection. Package library: GObject-2.0, Gio-2.0