Browse Source

Use new OSError errors

Ask Solem 8 years ago
parent
commit
a00fd489b0
4 changed files with 13 additions and 32 deletions
  1. 3 7
      celery/bin/multi.py
  2. 6 18
      celery/concurrency/asynpool.py
  3. 3 7
      celery/platforms.py
  4. 1 0
      docs/userguide/tasks.rst

+ 3 - 7
celery/bin/multi.py

@@ -263,9 +263,7 @@ class MultiTool:
     def signal_node(self, nodename, pid, sig):
         try:
             os.kill(pid, sig)
-        except OSError as exc:
-            if exc.errno != errno.ESRCH:
-                raise
+        except ProcessLookupError:
             self.note('Could not signal {0} ({1}): No such process'.format(
                 nodename, pid))
             return False
@@ -274,10 +272,8 @@ class MultiTool:
     def node_alive(self, pid):
         try:
             os.kill(pid, 0)
-        except OSError as exc:
-            if exc.errno == errno.ESRCH:
-                return False
-            raise
+        except ProcessLookupError:
+            return False
         return True
 
     def shutdown_nodes(self, nodes, sig=signal.SIGTERM, retry=None,

+ 6 - 18
celery/concurrency/asynpool.py

@@ -218,9 +218,7 @@ class ResultHandler(_pool.ResultHandler):
                 n = __read__(
                     fd, bufv[Hr:] if readcanbuf else bufv, 4 - Hr,
                 )
-            except OSError as exc:
-                if exc.errno not in UNAVAIL:
-                    raise
+            except (BlockingIOError, InterruptedError):
                 yield
             else:
                 if n == 0:
@@ -240,9 +238,7 @@ class ResultHandler(_pool.ResultHandler):
                 n = __read__(
                     fd, bufv[Br:] if readcanbuf else bufv, body_size - Br,
                 )
-            except OSError as exc:
-                if exc.errno not in UNAVAIL:
-                    raise
+            except (BlockingIOError, InterruptedError):
                 yield
             else:
                 if n == 0:
@@ -830,9 +826,7 @@ class AsynPool(_pool.Pool):
                 while Hw < 4:
                     try:
                         Hw += send(header, Hw)
-                    except Exception as exc:
-                        if getattr(exc, 'errno', None) not in UNAVAIL:
-                            raise
+                    except (BlockingIOError, InterruptedError) as exc:
                         # suspend until more data
                         errors += 1
                         if errors > 100:
@@ -846,9 +840,7 @@ class AsynPool(_pool.Pool):
                 while Bw < body_size:
                     try:
                         Bw += send(body, Bw)
-                    except Exception as exc:
-                        if getattr(exc, 'errno', None) not in UNAVAIL:
-                            raise
+                    except (BlockingIOError, InterruptedError) as exc:
                         # suspend until more data
                         errors += 1
                         if errors > 100:
@@ -895,18 +887,14 @@ class AsynPool(_pool.Pool):
                 while Hw < 4:
                     try:
                         Hw += send(header, Hw)
-                    except Exception as exc:
-                        if getattr(exc, 'errno', None) not in UNAVAIL:
-                            raise
+                    except (BlockingIOError, InterruptedError):
                         yield
 
                 # write body
                 while Bw < body_size:
                     try:
                         Bw += send(body, Bw)
-                    except Exception as exc:
-                        if getattr(exc, 'errno', None) not in UNAVAIL:
-                            raise
+                    except (BlockingIOError, InterruptedError):
                         # suspend until more data
                         yield
             finally:

+ 3 - 7
celery/platforms.py

@@ -136,7 +136,7 @@ class Pidfile:
         """Acquire lock."""
         try:
             self.write_pid()
-        except OSError as exc:
+        except FileExistsError:
             raise LockFailed(str(exc)).with_traceback(sys.exc_info()[2])
         return self
     __enter__ = acquire
@@ -466,9 +466,7 @@ def setgroups(groups):
         pass
     try:
         return _setgroups_hack(groups[:max_groups])
-    except OSError as exc:
-        if exc.errno != errno.EPERM:
-            raise
+    except PermissionError:
         if any(group not in groups for group in os.getgroups()):
             # we shouldn't be allowed to change to this group.
             raise
@@ -534,9 +532,7 @@ def maybe_drop_privileges(uid=None, gid=None):
         # ... and make sure privileges cannot be restored:
         try:
             setuid(0)
-        except OSError as exc:
-            if exc.errno != errno.EPERM:
-                raise
+        except PermissionError:
             pass  # Good: cannot restore privileges.
         else:
             raise RuntimeError(

+ 1 - 0
docs/userguide/tasks.rst

@@ -1122,6 +1122,7 @@ Example using reject when a task causes an out of memory condition:
         except OSError as exc:
             if exc.errno == errno.ENOMEM:
                 raise Reject(exc, requeue=False)
+            raise self.retry(exc, countdown=10)
 
         # For any other error we retry after 10 seconds.
         except Exception as exc: