is Python multithreading inefficient?

Source: Internet
Author: User
Send ICMP packets with scapy. 200 threads are opened, each thread sends a package and ends. Testing the speed of only 300 packets of/sec, is Python's multithreading efficiency is not high problem?
---------------------------------------
Later found not multi-threading problem, is scapy originally contracted to slow problem. Single thread scapy several packets per second, multithreading up to 60 per second. 3w per second in C.

Reply content:

Python has a full-lock presence (only one thread can execute at the same time) and does not take advantage of multicore advantages. Therefore, if your multithreaded process is CPU-intensive, that multithreading does not bring efficiency gains, on the contrary may be due to frequent thread switching, resulting in efficiency degradation, if it is IO-intensive, multi-threaded process can take advantage of IO blocking waiting idle time to execute other threads, improve efficiency. 200 threads are opened, each thread sends a package and ends.
=========
How about a different pose, black python? For example, the for loop is tens of thousands of times times slower than the C language. When different threads access resources at the same time, a protection mechanism is required, using the Gil (the Interpreter global lock) in Python. Intuitively, this is a global (from the interpreter's point of view) lock added to the interpreter. This means that for any Python program, no matter how many processors, there is always only one thread executing at any time. So, if there is no IO operation, multithreading in Python is less efficient than single threading.
You can see this Python's hardest question. Looking at the comments above, it's hard to understand why my Python program can run full 12 cores, whether with python2.6 or 2.7 or pypy. I do not know that the Python program can only run on a nuclear rumor is that these dabbler are transmitted to such a vicious situation. A program written by a Dabbler Python programmer is likely to be more performance than a dabbler C programmer. Gil is for a Python interpreter process, this is the truth, if the interpreter can explain the execution of multiple processes, there is no Gil problem, again, he will not cause you to run multiple interpreters on the same core. Scapy Use of Libcap library efficiency is very slow can try Scapy sendpfast is fast and right

Search keyword is Global interpreter Lock

Although I have not used py to write projects for many years, but I would like to ask:

> opened 200 threads, and each thread sent a package after the end.

This is really good ... Line threads is not money casually open ... found that the multi-thread of the process after the speed of a certain value, increase the number of threads and did not get the increase in speed, and then simply using multiprocess as far as possible to use the invariant as a shared data can be alleviated, in addition to pypy a lot faster, PyPy equipped with the JIT. Try Jython?
  • 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.