Reputation: 91
I'm trying to create boost python module and test producer & consumer problem.
But I faced some error and problems...
Below image shows my goal architecture.
I implemented boost python module
and here is python code snippet (main.py)
import threading
import datetime
import PyDataTest
queue = None
def thread_function():
print('[thread_function] start...')
while True:
print('[thread_function] try Pop')
data = queue.Pop()
print(f'[thread_function] Pop: {data}')
def wait_thread_function():
print('[wait_thread_function] start...')
wait_thread = threading.Thread(target=thread_function)
wait_thread.start()
wait_thread.join()
def gen_thread_function():
gen_thread_obj = PyDataTest.CustomThread()
gen_thread = threading.Thread(target=gen_thread_obj.Execute)
gen_thread.start()
gen_thread.join()
if __name__ == '__main__':
print('main start')
queue = PyDataTest.GetQueueInstance()
wait_thread_func = threading.Thread(target=wait_thread_function, args=())
wait_thread_func.start()
gen_thread_func = threading.Thread(target=gen_thread_function)
gen_thread_func.start()
timeVal = datetime.datetime.now()
while True:
currentTime = datetime.datetime.now()
if (currentTime - timeVal).seconds > 10:
print('Python Main running ...')
thread.join()
gen_thread.join()
I faced two problems
So my question is
My final goal is based on producer & consumer model and custom object type data shared via Queue object
Please help me
Thanks
Environment
Pop function code snippet
bool Pop(T& obj)
{
std::unique_lock<std::shared_mutex> ul(sm);
cv.wait(ul, [this] {return (!con.empty() || !isRun); });
if (!isRun)
{
return false;
}
if (!con.empty())
{
obj = con.front();
con.pop();
return true;
}
return false;
}
========================================================================
Error message when "gen_thread_function" running first
Upvotes: 2
Views: 269
Reputation: 91
Ok. I found how to solve that problems..
I found the way using scoped gil release. this question very helpful for me
When I run the thread, I face error windows above... it cause by I cannot join the thread in C++ module. I added thread join code inside run function the error goes away
I thought directly sharing a queue has lots of problems.. so I changed architecture. Queue object resident inside C++ module and expose the function for accessing queue. And it works nicely
It is same as solution 3, I solve the problem exposing C++ class
Thanks
Upvotes: 1