Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading in python3

im using multithreading in python3 with Flask as below. Would like to know if there is any issue in below code, and if this is efficient way of using threads

import _thread
COUNT = 0

class Myfunction(Resource):

    @staticmethod
    def post():
        global GLOBAL_COUNT
        logger = logging.getLogger(__name__)

        request_json = request.get_json()

        logger.info(request_json)

        _thread.start_new_thread(Myfunction._handle_req, (COUNT, request_json))
        COUNT += 1

        return Response("Request Accepted", status=202, mimetype='application/json')

    @staticmethod
    def _handle_req(thread_id, request_json):
        with lock:


            empID = request_json.get("empId", "")

            myfunction2(thread_id,empID)

api.add_resource(Myfunction, '/Myfunction')
like image 953
shiv455 Avatar asked Jun 22 '18 11:06

shiv455


People also ask

Does Python 3 support multithreading?

Python doesn't support multi-threading because Python on the Cpython interpreter does not support true multi-core execution via multithreading. However, Python does have a threading library.

Is Python 3 single threaded?

Python is NOT a single-threaded language. Python processes typically use a single thread because of the GIL. Despite the GIL, libraries that perform computationally heavy tasks like numpy, scipy and pytorch utilise C-based implementations under the hood, allowing the use of multiple cores.

How do you use threads in Python 3?

Creating Thread Using Threading ModuleDefine a new subclass of the Thread class. Override the __init__(self [,args]) method to add additional arguments. Then, override the run(self [,args]) method to implement what the thread should do when started.

Can we use multithreading in Python?

Multithreading in Python enables CPUs to run different parts(threads) of a process concurrently to maximize CPU utilization. Multithreading enables CPUs to run different parts(threads) of a process concurrently.

How do I run a multithread in Python?

To use multithreading, we need to import the threading module in Python Program. A start() method is used to initiate the activity of a thread. And it calls only once for each thread so that the execution of the thread can begin.


1 Answers

I think the newer module threading would be better suited for python 3. Its more powerful.

import threading

threading.Thread(target=some_callable_function).start()

or if you wish to pass arguments

threading.Thread(target=some_callable_function,
    args=(tuple, of, args),
    kwargs={'dict': 'of', 'keyword': 'args'},
).start()

Unless you specifically need _thread for backwards compatibility. Not specifically related to how efficient your code is but good to know anyways.

see What happened to thread.start_new_thread in python 3 and https://www.tutorialspoint.com/python3/python_multithreading.htm

like image 122
Zx4161 Avatar answered Sep 30 '22 01:09

Zx4161