使用 Gevent 记录多个协同例程/greenlets/微线程?
使用 Python 的 gevent 记录跨越多个正在运行的协同例程/微线程/Greenlets 的事件的最佳方法是什么?
我想要记录的示例事件可能包括创建新连接或删除连接套接字服务器。
沿着这个思路——“衍生”的协同例程是否可以记录到同一个文件?由于对该文件的潜在并发写入尝试,这是否是可取的?
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 connections 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?
如果这只是单个进程,那么不,他们不可能同时写入。 Greenlet 全部运行在同一个操作系统线程中,并且协同调度,一次只能运行一个。
...将导致 greenlet 一项一项地写入日志。
如果您有多个进程,我建议登录到
redis
,或者使用 zeromq 登录到专用守护进程。或者使用其他一些外部日志守护程序。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.
...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.