Python: Pass or Sleep for long running processes?

前端 未结 7 1383
情书的邮戳
情书的邮戳 2020-11-30 05:10

I am writing an queue processing application which uses threads for waiting on and responding to queue messages to be delivered to the app. For the main part of the applica

7条回答
  •  执念已碎
    2020-11-30 05:52

    You don't give much context to what you are really doing, but maybe Queue could be used instead of an explicit busy-wait loop? If not, I would assume sleep would be preferable, as I believe it will consume less CPU (as others have already noted).

    [Edited according to additional information in comment below.]

    Maybe this is obvious, but anyway, what you could do in a case where you are reading information from blocking sockets is to have one thread read from the socket and post suitably formatted messages into a Queue, and then have the rest of your "worker" threads reading from that queue; the workers will then block on reading from the queue without the need for neither pass, nor sleep.

提交回复
热议问题