zivid / zivid-python

Official Python package for Zivid 3D cameras
BSD 3-Clause "New" or "Revised" License
43 stars 14 forks source link

some question about Multi-threading #231

Open mihudaner opened 1 year ago

mihudaner commented 1 year ago

When I was developing using Python, I didn't want to block opening the camera or collecting data, so I placed the connection camera in a sub thread. However, I don't know why, the entire process is blocked, so connecting the camera shouldn't take up a lot of resources, right? But why is the entire process blocked?

eskaur commented 1 year ago

Which version of zivid-python are you using? (pip show zivid)

mihudaner commented 1 year ago

image

this is my easy test demo with threading belowed Running the connection camera print hi on my computer will be blocked


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/7/20 7:13
# @Author  : mihudan~
# @File    : test_load_zivid.py
# @Description :
import time
import zivid
import threading

def print_hi():
    while True:
        time.sleep(0.01)
        print(f'Hi, ')

if __name__ == '__main__':
    t1 = threading.Thread(target=zivid.Application().connect_camera)
    t2 = threading.Thread(target=print_hi)
    t2.start()
    time.sleep(3)
    t1.start()

    t2.join()
mihudaner commented 1 year ago

When capturing, the interface of my pyqt will also become very stiff, but I have already placed it in a sub thread for execution

eskaur commented 1 year ago

Aha, actually there is no released version of zivid-python that supports multithreading. We recently fixed this on master branch: https://github.com/zivid/zivid-python/pull/222

So you have two options now:

mihudaner commented 1 year ago

So the lower version is compatible, right? I would also like to know the underlying cause of this bug, as I happen to be learning about multithreading and multiprocessing. Why does this thread affect other threads? I want to ask for some adviceO(∩_∩)O

eskaur commented 1 year ago

So the lower version is compatible, right?

Not sure what you mean. The latest version on PyPI is 2.9.0.2.9.0, and that one does not include the fix. However, it is fixed on master branch so you could install pre-release by checking out the git repository and installing from that.

As to why it failed: The issue is that we would apply the Global Interpreter Lock ("GIL") whenever some work was passed to the Zivid SDK. This locks all threads. See here for more details: https://github.com/zivid/zivid-python/pull/222

mihudaner commented 1 year ago

Not sure what you mean

I mean, the lower version can use threading, right? /

As to why it failed: The issue is that we would apply the Global Interpreter Lock ("GIL") .......

thank you very much indeed

eskaur commented 1 year ago

I mean, the lower version can use threading, right?

You mean if 2.8.0.2.9.0 can use threading? Sorry, no :)

mihudaner commented 1 year ago

I understand. Thank you again (my English is not very good (●'◡'●))