1 parent
8e3b4a0bf3
commit
d40481acc5
1 file changed
+23
-1
+23
-1
@@ -1,5 +1,6 @@
|
|||||||
# YOLOv5 general utils
|
# YOLOv5 general utils
|
||||||
|
|
||||||
|
import contextlib
|
||||||
import glob
|
import glob
|
||||||
import logging
|
import logging
|
||||||
import math
|
import math
|
||||||
@@ -7,6 +8,7 @@ import os
|
|||||||
import platform
|
import platform
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
|
import signal
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
import urllib
|
import urllib
|
||||||
@@ -34,6 +36,26 @@ cv2.setNumThreads(0) # prevent OpenCV from multithreading (incompatible with Py
|
|||||||
os.environ['NUMEXPR_MAX_THREADS'] = str(min(os.cpu_count(), 8)) # NumExpr max threads
|
os.environ['NUMEXPR_MAX_THREADS'] = str(min(os.cpu_count(), 8)) # NumExpr max threads
|
||||||
|
|
||||||
|
|
||||||
|
class timeout(contextlib.ContextDecorator):
|
||||||
|
# Usage: @timeout(seconds) decorator or 'with timeout(seconds):' context manager
|
||||||
|
def __init__(self, seconds, *, timeout_message="", suppress_timeout_errors=True):
|
||||||
|
self.seconds = int(seconds)
|
||||||
|
self.timeout_message = timeout_message
|
||||||
|
self.suppress = bool(suppress_timeout_errors)
|
||||||
|
|
||||||
|
def _timeout_handler(self, signum, frame):
|
||||||
|
raise TimeoutError(self.timeout_message)
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
signal.signal(signal.SIGALRM, self._timeout_handler) # Set handler for SIGALRM
|
||||||
|
signal.alarm(self.seconds) # start countdown for SIGALRM to be raised
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
signal.alarm(0) # Cancel SIGALRM if it's scheduled
|
||||||
|
if self.suppress and exc_type is TimeoutError: # Suppress TimeoutError
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def set_logging(rank=-1, verbose=True):
|
def set_logging(rank=-1, verbose=True):
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
format="%(message)s",
|
format="%(message)s",
|
||||||
@@ -86,7 +108,7 @@ def check_online():
|
|||||||
# Check internet connectivity
|
# Check internet connectivity
|
||||||
import socket
|
import socket
|
||||||
try:
|
try:
|
||||||
socket.create_connection(("1.1.1.1", 443), 5) # check host accesability
|
socket.create_connection(("1.1.1.1", 443), 5) # check host accessibility
|
||||||
return True
|
return True
|
||||||
except OSError:
|
except OSError:
|
||||||
return False
|
return False
|
||||||
|
|||||||
Reference in new issue
Block a user