Python Tornado asynchronous performance test

Source: Internet
Author: User

Test two interfaces

#-*-coding:utf-8-*-Import TimeImportTornado.webImportTornado.genImportTornado.ioloop fromTornado.concurrentImportRun_on_executor fromConcurrent.futuresImportThreadpoolexecutorclassSyncHandler (tornado.web.RequestHandler):defGet (self, *args, * *Kwargs): Time.sleep (5# Sleep is used to simply refer to a time-consuming IO operation Self.write ("Hello World in sync")classAsynchandler (Tornado.web.RequestHandler): Executor= Threadpoolexecutor (5) @tornado. Gen.coroutinedefGet (self): Resp=yieldSelf.sleep_for_result () self.write (resp) @run_on_executordefSleep_for_result (self): Time.sleep (5)        return 'The asynchronous Hello World'Application=Tornado.web.Application ([(R'/sync', SyncHandler), (R'/async', Asynchandler),])if __name__=="__main__":     fromTornado.optionsImportoptions, define define ("Port", default=8080, help="running in 8080", type=int) Http_server=tornado.httpserver.HTTPServer (application) http_server.bind (options.port) Http_server.start (1) # Number of Processes Tornado.ioloop.IOLoop.instance (). Start ()

Start the Tornado service.

This does not use the AB test, testing performance with a more flexible code thread pool, and using the thread pool to request interfaces concurrently

#Coding=utf8ImportRequestsImport Time fromTomorrowImportThreads@threads (10)defTest_sync ():PrintRequests.get ('Http://127.0.0.1:8080/sync'). Content +'        '+ Time.strftime ('%h:%m:%s') @threads (10)defTest_async ():PrintRequests.get ('Http://127.0.0.1:8080/async'). Content +'        '+ Time.strftime ('%h:%m:%s') [Test_sync () forIinchRange (100)]#[test_async () forIinchRange (100)]

The synchronization method is tested as follows:

Look to see that when the 10 thread requests the synchronization interface, it is every 5 seconds to receive processing to complete a request. The tornado process set in the program is 1, and if the number of tornado services is increased to 4, 4 synchronization requests can be processed every 5 seconds.

The asynchronous method is tested as follows:

As you can see, when the 10 thread requests the asynchronous interface, it is able to process 5 requests every 5 seconds because the number of Threadpoolexecutor (5) set in the code is 5. If set to 8, 8 requests can be processed every 5 seconds.

In doing Unicom central bank credit based on the user authorization of the login system, you need to verify the user submitted account password verification code, plus the use of proxy IP time is very unstable, verify the user submitted account password is able to login to the three-party website when it takes a lot of time, you need to use tornado this asynchronous way, Otherwise, it takes a long time for the user to submit the account password to get feedback, and the user experience is bad.

If you use Django, you can handle 1 requests every 5 seconds, and other requests must wait for the last request to finish before they are processed.

But Django uses UWSGI deployment, which does not behave as poorly as its own service, and typically sets the number of processes and the number of threads when deployed with UWSGI, and can process more requests every 5 seconds after deployment.

Python tornado asynchronous performance test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.