Queue a callback to be run on the main thread. The callback is added to the queue first, then a wake-up byte is written to the pipe. If the pipe write fails (buffer full), it's safe to ignore because the main thread will eventually drain the queue when it reads other
(self, callback, *args)
| 173 | return self._read_fd |
| 174 | |
| 175 | def defer(self, callback, *args): |
| 176 | """Queue a callback to be run on the main thread. |
| 177 | |
| 178 | The callback is added to the queue first, then a wake-up byte |
| 179 | is written to the pipe. If the pipe write fails (buffer full), |
| 180 | it's safe to ignore because the main thread will eventually |
| 181 | drain the queue when it reads other wake-up bytes. |
| 182 | """ |
| 183 | self._queue.put(partial(callback, *args)) |
| 184 | try: |
| 185 | os.write(self._write_fd, b'\x00') |
| 186 | except OSError: |
| 187 | # Pipe buffer full (EAGAIN/EWOULDBLOCK) - safe to ignore |
| 188 | # The main thread will still process the queue |
| 189 | pass |
| 190 | |
| 191 | def run_callbacks(self, _fileobj, max_callbacks=50): |
| 192 | """Run queued callbacks. Called when the pipe is readable. |