Jump to content

Python3, Why does this bypass GIL?

Poet129
Go to solution Solved by Sauron,
7 minutes ago, Poet129 said:

I've run it... but ok.

You've run it and... what? If it was faster than just doing sleep x times that's because sleep doesn't actually do anything and other threads can be run while it's waiting.

I wrote this code to bypass GIL, but using things other than sleep seems to make it not work:

from timeit import timeit
from time import sleep
from threading import Thread
from os import cpu_count
openfile = open("TempFile.txt", "w")
openfile.write("")
openfile.close()
def run(x):
    sum = x + x
    sleep(1)
    openfile = open("TempFile.txt", "a")
    openfile.write(str(sum) + "\n")
    openfile.close()
def runCPU():
    x = 0
    threads = []
    while (x < cpu_count()):
        t = Thread(target=run, args=(x,))
        t.start()
        x += 1
        threads.append(t)
    for t in threads:
        t.join()
time = timeit(runCPU, number=1)
print(int(time))

 

Link to comment
Share on other sites

Link to post
Share on other sites

What makes you think this "bypasses" the GIL? If you have problems with the GIL you should just use multiprocessing or a python interpreter that isn't CPython.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

57 minutes ago, Sauron said:

What makes you think this "bypasses" the GIL? If you have problems with the GIL you should just use multiprocessing or a python interpreter that isn't CPython.

I've run it... but ok.

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Poet129 said:

I've run it... but ok.

You've run it and... what? If it was faster than just doing sleep x times that's because sleep doesn't actually do anything and other threads can be run while it's waiting.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Sauron said:

You've run it and... what? If it was faster than just doing sleep x times that's because sleep doesn't actually do anything and other threads can be run while it's waiting.

Okay, that's probably right thanks.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×