Browse Source

def os_read_non_blocking()

Fabian Peter Hammerle 5 years ago
parent
commit
67800f748c
1 changed files with 7 additions and 0 deletions
  1. 7 0
      rc.xsh

+ 7 - 0
rc.xsh

@@ -31,6 +31,7 @@ $RIGHT_PROMPT = '{gitstatus}{env_name: {}}'
 import datetime as dt
 import os
 import re
+import select
 import shutil
 import subprocess
 import sys
@@ -137,6 +138,12 @@ def locate(*patterns, match_all=True, ignore_case=True):
         params.insert(0, '--ignore-case')
     return $(locate @(params) -- @(patterns)).split('\n')[:-1]
 
+def os_read_non_blocking(fd, buffer_size_bytes=8*1024, timeout_seconds=0.1):
+    if fd in select.select([fd], [], [], timeout_seconds)[0]:
+        return os.read(fd, buffer_size_bytes)
+    else:
+        return None
+
 def timestamp_now_utc():
     return dt.datetime.utcnow().replace(tzinfo=dt.timezone.utc)