If you time just the send_to_slack inside the emit what sort of times do
you get?
AM
On 05/31/2016 11:56 PM, Рушан Шаймарданов wrote:
> I have an application based on gevent. It uses custom logger to log
> errors to slack channel. The problem is that with this logger enabled,
> application works about 4 times slower. To demonstrate this, I wrote
> simple script
>
> |
> importgevent.monkey
> gevent.monkey.patch_all()
> importgevent.pool
> fromfunctools importpartial
> importlogging
> importtime
> importurllib2
> importurllib
>
>
>
>
> defsend_to_slack(token,channel,username,text):
> data ={
> 'token':token,
> 'text':text,
> 'channel':channel,
> 'username':username
> }
> fork,v indata.items():
> data[k]=unicode(v).encode('utf-8')
> req =urllib2.Request('
https://slack.com/api/chat.postMessage',
> urllib.urlencode(data))
> resp =urllib2.urlopen(req)
>
>
> classSlackHandler(logging.Handler):
>
>
> def__init__(self,token,channel,username):
> super(SlackHandler,self).__init__()
> self.token =token
> self.channel =channel
> self.username =username
>
>
> defemit(self,record):
> send_to_slack(
> self.token,
> self.channel,
> self.username,
> self.format(record)
> )
>
>
> logger =logging.getLogger(__name__)
> h =SlackHandler('mytoken','#mychan','server-logs')
> h.setLevel(logging.ERROR)
>
>
> logger.addHandler(h)
>
>
> send =partial(send_to_slack,'mytoken',
> '#mychan','server-logs')
>
>
>
>
> pool =gevent.pool.Pool(10)
>
>
> text =['text']*10
>
>
> start =time.time()
> pool.map(logger.error,text)
> printtime.time()-start
>
>
> start =time.time()
> pool.map(send,text)
> printtime.time()-start
>
> |
>
>
> The result was:
>
> |
> 4.66483712196
> 0.50646686554
> |
>
>
> so, sending messages using logging module is 10 times slower then
> sending them directly. Seems like logging module is not monkeypatched by
> gevent correctly, or so.
>
> My question is: how to properly use logging module in gevent applications?
>
> --
> You received this message because you are subscribed to the Google
> Groups "gevent: coroutine-based Python network library" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
gevent+un...@googlegroups.com
> <mailto:
gevent+un...@googlegroups.com>.
> For more options, visit
https://groups.google.com/d/optout.