python - flask threading on triggering a different process -


in route request in flask, how trigger thread start , have continue right away next line. in code below, how print "onto next already" occur right after new thread spawned, rather wait ~10 sec finish?

thanks!

def consumer(searchvalue,assigneduniqueid):     print "searching..."     print "running thread! args:", (searchvalue, assigneduniqueid)     time.sleep(10)     print "done!"  @app.route('/_search') def add_numbers():     assigneduniqueid = str(uuid.uuid4())     thread(target=consumer(searchvalue,assigneduniqueid)).start()     print "onto next already"     return jsonify(result="33434443",                        assigneduniqueid = assigneduniqueid                ) 

sorry, more comment answer, i'm posting here because bit large comment system.

this kind of bug can hard track in flask given extensive use of thread-local storage , other magic.

in web applications multitasking handled use of queues instead of threads, web application queing tasks , background daemons (workers) doing job. approach scales lot better:

  • workers can live in other machines
  • the number of workers running in parallel easier control number of running threads. i've seen small traffic surges bringing server halt when using threads.
  • it easier synchronize , or handle concurrency

specially popular solutions involving amqp or key/value databases redis or memcached. amqp in python, take @ celery, use lot in combination django.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -