Logging across multiple co-routines / greenlets / microthreads with Gevent?
What is the best approach to logging events that span multiple running co-routines / microthreads / Greenlets using Python's gevent?
Example events I would like to log could include the creation of new conne开发者_运维知识库ctions or the dropping of connections to a socket server.
Along this line of thought - is it possible for 'spawned' co-routines to log to the same file? Is that even advisable due to potential concurrent write attempts to that file?
If this is just single process, then no, they can't possibly write at the same time. Greenlets all run in the same OS thread and are scheduled cooperatively, only one can be running at a time.
with open('log', 'w') as log:
jobs = [gevent.spawn(log.write, 'event %d' % i) for i in range(10)]
gevent.joinall(jobs)
...would result in the greenlets writing to the log one by one.
If you have multiple processes, I'd suggest logging to redis
, or maybe using zeromq to log to a dedicated daemon. Or use some other external logging daemon.
精彩评论