Conditions | 14 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like EventQueueEmptyEventHandler.handle() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """ |
||
39 | @staticmethod |
||
40 | def handle(_event, _event_data, spawner): |
||
41 | """ |
||
42 | Handles an empty event queue event. |
||
43 | |||
44 | :param _event: The event. |
||
45 | :param * _event_data: Not used. |
||
46 | :param enarksh.spawner.Spawner.Spawner spawner: The spawner. |
||
47 | """ |
||
48 | del _event, _event_data |
||
49 | |||
50 | # List with all file descriptors for reading. |
||
51 | # Add the file descriptor for waking up select when a signal has been received. |
||
52 | read = [EventQueueEmptyEventHandler._wake_up_pipe[0]] |
||
53 | |||
54 | # Add the sockets for incoming messages to the list of read file descriptors. |
||
55 | zmq_fds = set() |
||
56 | for socket in Message.message_controller.end_points.values(): |
||
57 | if socket.type in [zmq.PULL, zmq.REP]: |
||
2 ignored issues
–
show
|
|||
58 | zmq_fd = socket.get(zmq.FD) |
||
1 ignored issue
–
show
|
|||
59 | read.append(zmq_fd) |
||
60 | zmq_fds.add(zmq_fd) |
||
61 | |||
62 | # Add the job handlers to the list of read file descriptors. |
||
63 | for pid, job_handler in spawner.job_handlers.items(): |
||
64 | fd_stdout = job_handler.stdout |
||
65 | if fd_stdout >= 0: |
||
66 | read.append(fd_stdout) |
||
67 | fd_stderr = job_handler.stderr |
||
68 | if fd_stderr >= 0: |
||
69 | read.append(fd_stderr) |
||
70 | |||
71 | # Unblock interrupts. |
||
72 | signal.pthread_sigmask(signal.SIG_UNBLOCK, {signal.SIGCHLD, signal.SIGHUP}) |
||
73 | |||
74 | try: |
||
75 | # Wait for a fd becomes available for read or wait for an interrupt. |
||
76 | read, _, _ = select.select(read, [], []) |
||
77 | |||
78 | except InterruptedError: |
||
79 | # Ignore Interrupted system call errors (EINTR) in the select call. |
||
80 | # Note: In Python 3.5 EINTR will not occur any more. Nevertheless, we must only allow interrupts here to |
||
81 | # prevent unwanted side effect of the signal handlers. |
||
82 | read = [] |
||
83 | |||
84 | # Block all interrupts to prevent interrupted system call errors (EINTR). |
||
85 | signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGCHLD, signal.SIGHUP}) |
||
86 | |||
87 | zmq_event = False |
||
88 | for fd in read: |
||
89 | if fd in zmq_fds: |
||
90 | # fd of the message queue is ready to receive data. |
||
91 | zmq_event = True |
||
92 | elif fd == EventQueueEmptyEventHandler._wake_up_pipe[0]: |
||
93 | os.read(EventQueueEmptyEventHandler._wake_up_pipe[0], 512) |
||
94 | else: |
||
95 | # fd of one or more job handlers are ready to receive data. |
||
96 | for job_handler in spawner.job_handlers.values(): |
||
97 | if fd == job_handler.stdout: |
||
98 | job_handler.read(fd) |
||
99 | if fd == job_handler.stderr: |
||
100 | job_handler.read(fd) |
||
101 | |||
102 | if zmq_event: |
||
103 | spawner.zmq_incoming_message_event.fire() |
||
104 | else: |
||
105 | Event.event_controller.event_queue_empty.fire() # XXX test queue is empty |
||
106 | |||
108 |