Meme Game server Development Diary (ii) bypass Gil start a multithreaded python environment

Source: Internet
Author: User
Tags add numbers

When it comes to Python and multithreading, it's easy to think that Gil,gil means you can't take advantage of multiple CPUs as long as you're using Python as a multithreaded program.

After some unsuccessful attempts, I also once thought that Gil was no solution. We have even turned our attention to the IronPython python, but in fact it may be more problematic, for example, we are not familiar with Mono,mono and have not reached full maturity.


Until a friend in the QQ group of Skynet introduced another way to load so, things have changed 180 degrees.


Here's how:

1, compile Python source code, compile with parameter--enable-shared, compile into so dynamic link library.

2, find so, copy n parts, respectively named Libpython_1.so, Libpython_2.so, ... Wait a minute

3, using the C language, the LDFCN library under Linux, dynamically load these so libraries, LDFCN can ensure that the same name global variables do not conflict.

(c language source, to be sorted)


(compilation considerations, to be sorted out)


(Set environment variables, to be sorted)


With the simple PY script test, you will find that the script is actually running independently. However, if import time is used, it can cause a segment error.

The reason is that in this way, later import of the various Python libraries, if it is a C language extension, then because of the repetition of the conflict.

The workaround is to add--enable-time when compiling Python, and compile the time and Python libraries together to solve this problem.

(another option is to find the time source code, add numbers after the name of the library, and also compile n copies of Time_1, time_2 ... And so on, in the script also separately import different time library, can. )


Because our Python environment prohibits multiple threads from being started within a script, you can add the compile parameter--without-threads, which can also slightly improve the efficiency of the operation.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Meme Game server Development Diary (ii) bypass Gil start a multithreaded python environment

Related Article

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.