Does anyone know if Pythonkit supports subthreads?

Anyone know how to do that let Pythonkit run in a subthread? I really create a serial subthread for Pythonkit but it's always occur " #1 0x0000000105729c54 in _pthread_cond_wait ()"

All of my code and error is below:

What is Python code doing? Try with a very simple Python code.

Possibilities (sorry just a quick shot, and more for your question in the other topic):

  • Use an actor to sequentialize access to Python functions, all Python stuff then being executed sequentially. (Maybe the actor has to be executed on the main thread, maybe someone can guess here, could there be a problem?)
  • Sequentialize calls of Python functions as before, but do some parallel stuff as it works today on the Python side — but this might get complicated.
  • For batch processing: Do not use PythonKit, but process bulks by starting Python several times as an external program.
  • Wait for Python to remove the Global Interpreter Lock (GIL) — this seems to be planned, question is will this really work with the (current) PythonKit?
  • Wait even longer and see a) if Mojo can process your Python code and b) if there is a binding from Swift to Mojo.

my code is doing batch processing image by call cv2 and numpy lib

My python code can run in main thread. But it will cause my app stuck every time. It's batch processing images by call cv2 and numpy lib. It's seem no way to solve this problem now. So sad.

If it is s batch run, just start Python as an external program. You then could do this in parallel with suitable batch sizes (every Python instance working on a pile of files), something that is (currently?) not possible with PythonKit. The parallel execution should save you a lot of time, and Python startup is neglectable in comparison. (BTW you should work with e.g. a (mini)conda environment and call the Python executable within your environment, but I guess you know about that.)

but I want to publish the app to Mac app store, I don't know if I will be reject by use subprocess in my app that build by python.

I mean try a minimal Python code to run with your current setup. If it still doesn't work then something is wrong with your setup and then it would be easier to see what exactly is wrong having a minimal python app. OTOH if it does work that's a different story, in that case I'd recommend to distill Python code to get it from not working to working (at which moment you'll realise what exactly caused the trouble).

I’ve been using PythonKit for running Swift in Python instead of Python in Swift. I suspect what might be happening for you is no GIL handling, which I had to add to get my projects working.

It’s a bit rough and the GIL state handling coverage isn't complete, but it might be worth trying my fork to see if that helps at all.

2 Likes