Python: drawbacks to using `signal.alert` to timeout I/O? -
What is the harm to Python I / O timeout?
I ask because I have found that socket.settimeout
is not completely reliable [0] and I need better control over timeouts for various tasks. [1].
Therefore, as far as I can tell, there are drawbacks:
- Added signal call overhead (but if you are doing I / O, this is a surprise Should not be)
- This can be "more complex" if using application threading (but if threading is already being used, then it would be easier to increase a monitor thread).
Am I missing anything?
[0]: For example, it will not be time to slow down DNS lookups. For example, I want some time to finish reading some time, while others should have more time to read.
You probably will not get away from using signals and threads. From:
... Only the main thread can set a new signal handler, and there will be only one in getting the main thread signal (this is implemented by the Python signal module, even so That if the built-in thread implementation supports sending signal to different threads) ...
As far as the drawbacks occur, the interpreter only handles the signal handler in the main thread Interp to Referrer will return by the reference. If you have long-running C extension calls (SQL queries, system IOO operations, etc.), then SIGINT and SIGTERM will not be managed until the signal is back. If you have a hard time that you are applying, then it will not help. To be around this, I have the best way to distribute the work process in a child's process, use Sigil to kill the hair process from time to time, and then replace the hair process (a custom process pool ).
In addition, if you want to use the signal handler as a way to jump out of the code block through asynchronous ups-off exceptions, there is no guarantee for the discretion of your program state. . Exceptions can also end up lifting themselves into unexpected places.
For your needs with socket programming, you should start with a loop to vote for non-blocked I / O and voting for readiness. Or you can use modules or non-standard libraries which remove many details.
Comments
Post a Comment