Reputation: 65
Recently, I view the code of gunicorn
.
I have a very confusing issue, what is the effect of WorkerTmp in worker/workertmp.py
?
gunicorn version: 19.6.0
Upvotes: 0
Views: 537
Reputation: 39
WorkerTmp is the heart beating way between Arbiter and Worker. Every worker is related to one tmp (instance of WorkerTmp), and WorkerTmp contains a tempfile(_tmp).
As we can see, worker (for example, sync.SyncWorker
) will call WorkerTmp.notify intervally, which will modify the metadata info about tempfile, code as below:
def notify(self):
try:
self.spinner = (self.spinner + 1) % 2
os.fchmod(self._tmp.fileno(), self.spinner)
except AttributeError:
# python < 2.6
self._tmp.truncate(0)
os.write(self._tmp.fileno(), b"X")
Come back to Arbiter, arbiter will check whether any worker lose connection in main loop(arbiter.py:197). but how? as line 477 in arbiter.py shows:
if time.time() - worker.tmp.last_update() <= self.timeout:
check the interval between now and tempfile's last update time, if the interval is longer than configured timeout, we think the worker has lost connection. Now the key point is method tmp.last_update
, code shows everything:
def last_update(self):
return os.fstat(self._tmp.fileno()).st_ctime
os.fchmod
will change the st_ctime
of file. we can use "stat" to check the three time attributes of file.
Upvotes: 1