LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   CPython GIL threading problem. (https://www.linuxquestions.org/questions/programming-9/cpython-gil-threading-problem-4175500139/)

fantasy1215 04-01-2014 02:57 AM

CPython GIL threading problem.
 
We work on a linux server. When I type python, the information is as follow:
Code:

Python 2.6.6 (r266:84292, May  1 2012, 13:52:42)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2

I think this is a CPython version,isn't it?
When I write a threading program, I found some threads would starve.
After some google, I realize there is a thing called GIL(Global Interpreter Lock) in CPython.
If I want to use multithreading in python, What would you suggest? Thanks in advance!

example code that create a thread, and one of the thread would starve in my linux.
Code:

import time
import threading, thread

class thread_reload_config(threading.Thread):
    def run(self):
        while True:
            print("thread to reaload logging config file")
            time.sleep(5)

def main():
    print("enter main function")
    t = thread_reload_config()
    t.start()
   
    while True:
        print("still running")
        time.sleep(3)
if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        exit(0)


frostschutz 04-02-2014 05:08 PM

Your example code seems to work fine for me, except it doesn't handle keyboard interrupts well at all. You should avoid threading if possible, or if you're serious about it, maybe another language is more appropriate. For example Go uses a lot of concurrency and threading and stuff. But it's very, very different from Python.


All times are GMT -5. The time now is 09:02 PM.