jart / cosmopolitan

build-once run-anywhere c library
ISC License
18.35k stars 630 forks source link

python.com test_threading.py should pass #747

Open ingenieroariel opened 1 year ago

ingenieroariel commented 1 year ago

This may be a somewhat private api but it is used by Django.

https://docs.python.org/3/library/_thread.html#thread.get_ident

It is used here:

https://github.com/django/django/blob/stable/2.2.x/django/db/backends/base/base.py#L8

import threading
import _thread
        # Thread-safety related attributes.
        self._thread_sharing_lock = threading.Lock()
        self._thread_sharing_count = 0
        self._thread_ident = _thread.get_ident()
jart commented 1 year ago

Actually Portable Python predates Cosmopolitan Threads support. You might want to try one of @ahgamut's more recent builds of Python. See https://github.com/shmup/awesome-cosmopolitan which lists https://github.com/ahgamut/cpython/tree/cosmo_py311 as the most recent. I'd be happy to merge a workaround for Cosmo's Python 3.6 if you send one. Thanks!

ahgamut commented 1 year ago

@ingenieroariel you might try the newer Python311 or try building python.com from this experimental branch: https://github.com/ahgamut/cosmopolitan/tree/python36-threads

I checked that the _thread module does work with a simple example, and moreover _thread.get_ident() does not error out

jart commented 1 year ago

Wow @ahgamut awesome change! Reopening since we have an opportunity to get threading support fully working in the mono repo.

ingenieroariel commented 1 year ago

test_threading.py times out after 90 seconds. But when I ran it directly, it passed:

[x@nunez:~/superconfigure/cosmopolitan]$ o//third_party/python/pythontester.com -m test.test_threading
..............................................................................................................................s................task <thread 0> will run for 18.1 usec
1 tasks are running
task <thread 1> will run for 98.7 usec
task <thread 0> done
2 tasks are running
task <thread 2> will run for 56.6 usec
<thread 0> is finished. 1 tasks are running
task <thread 3> will run for 18.8 usec
2 tasks are running
task <thread 4> will run for 66.3 usec
3 tasks are running
task <thread 5> will run for 87.7 usec
task <thread 2> done
task <thread 3> done
task <thread 8> will run for 16.5 usec
<thread 2> is finished. 2 tasks are running
task <thread 6> will run for 63.4 usec
task <thread 7> will run for 47.5 usec
task <thread 1> done
<thread 3> is finished. 1 tasks are running
task <thread 9> will run for 96.6 usec
waiting for all tasks to complete
2 tasks are running
<thread 1> is finished. 1 tasks are running
2 tasks are running
3 tasks are running
task <thread 6> done
<thread 6> is finished. 2 tasks are running
task <thread 8> done
3 tasks are running
<thread 8> is finished. 2 tasks are running
3 tasks are running
task <thread 9> done
<thread 9> is finished. 2 tasks are running
task <thread 7> done
<thread 7> is finished. 1 tasks are running
task <thread 4> done
2 tasks are running
<thread 4> is finished. 1 tasks are running
task <thread 5> done
<thread 5> is finished. 0 tasks are running
all tasks done
.with 1MB thread stack size...
task <thread 0> will run for 8.4 usec
task <thread 1> will run for 40.4 usec
1 tasks are running
task <thread 2> will run for 19.5 usec
task <thread 3> will run for 16.6 usec
2 tasks are running
task <thread 0> done
task <thread 6> will run for 42.5 usec
3 tasks are running
task <thread 7> will run for 52.6 usec
task <thread 4> will run for 58.1 usec
task <thread 5> will run for 63.1 usec
task <thread 1> done
task <thread 2> done
waiting for all tasks to complete
task <thread 9> will run for 71.4 usec
<thread 0> is finished. 2 tasks are running
task <thread 8> will run for 21.5 usec
<thread 1> is finished. 1 tasks are running
<thread 2> is finished. 0 tasks are running
1 tasks are running
2 tasks are running
3 tasks are running
task <thread 3> done
<thread 3> is finished. 2 tasks are running
3 tasks are running
task <thread 7> done
<thread 7> is finished. 2 tasks are running
task <thread 6> done
<thread 6> is finished. 1 tasks are running
2 tasks are running
3 tasks are running
task <thread 4> done
<thread 4> is finished. 2 tasks are running
3 tasks are running
task <thread 8> done
<thread 8> is finished. 2 tasks are running
task <thread 5> done
<thread 5> is finished. 1 tasks are running
task <thread 9> done
<thread 9> is finished. 0 tasks are running
all tasks done
.with 256kB thread stack size...
task <thread 0> will run for 13.5 usec
1 tasks are running
task <thread 1> will run for 14.8 usec
task <thread 0> done
2 tasks are running
task <thread 2> will run for 81.4 usec
task <thread 3> will run for 17.5 usec
<thread 0> is finished. 1 tasks are running
task <thread 4> will run for 80.2 usec
task <thread 5> will run for 94.2 usec
task <thread 1> done
2 tasks are running
task <thread 8> will run for 72.4 usec
task <thread 9> will run for 6.3 usec
waiting for all tasks to complete
3 tasks are running
task <thread 3> done
task <thread 6> will run for 98.9 usec
task <thread 7> will run for 10.3 usec
<thread 1> is finished. 2 tasks are running
<thread 3> is finished. 1 tasks are running
2 tasks are running
3 tasks are running
task <thread 2> done
<thread 2> is finished. 2 tasks are running
3 tasks are running
task <thread 4> done
<thread 4> is finished. 2 tasks are running
task <thread 5> done
task <thread 8> done
3 tasks are running
<thread 5> is finished. 2 tasks are running
task <thread 9> done
<thread 8> is finished. 1 tasks are running
2 tasks are running
<thread 9> is finished. 1 tasks are running
2 tasks are running
task <thread 7> done
<thread 7> is finished. 1 tasks are running
task <thread 6> done
<thread 6> is finished. 0 tasks are running
all tasks done
........s...
----------------------------------------------------------------------
Ran 157 tests in 9.424s

OK (skipped=2)