get ( timeout = 2 ) return 'Result is %d ' % r if _name_ = '_main_' : _pool = Pool ( processes = 4 ) try : # insert production server deployment code app. apply_async ( expensive_function ,) r = f. One can use the same idea for a website as shown below in the Flask app example: from multiprocessing import Pool from flask import Flask app = Flask ( _name_ ) _pool = None def expensive_function ( x ): # import packages that is used in this function # do your expensive time consuming process return x * x ( '/expensive_calc/' ) def route_expcalc ( x ): f = _pool. In the main block we start a pool of 4 processes. The above snippet is copied from the multiprocessing documentation, and is fairly selfÄ®xplanatory. apply_async ( expensive_function, ) print result. Here is a simple introduction to multiprocessing: from multiprocessing import Pool def expensive_function ( x ): # do your expensive time consuming process return x * x if _name_ = '_main_' : # start 4 worker processes pool = Pool ( processes = 4 ) # evaluate "f(10)" asynchronously result = pool. Is to use multiprocessing as a task queue. The Queue class in this module implements all the. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. Some amount of setup and if you want to avoid, try using the following taskÄepending on the application at hand, Celery might be an overkill. The queue module implements multi-producer, multi-consumer queues. Then you put numbers from 1 to 5 into the queue using method. First, you make a FIFO queue object and call it. In this example, the Thread method takes two inputs, the function name ( target) and its arguments ( args ), as a tuple. The following example shows a simple FIFO queue loading and unloading. We use the Thread constructive method to create a thread instance. It is conventional to use a task queue such as Celery. First, we need to import the threading module, a high-level threading module with various useful features. When you have computationally intensive tasks in your website (or scripts), Use multiprocessing module as a task queue, and over come GIL in python.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |