forked from luxonis/depthai-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataQueueBindings.cpp
More file actions
126 lines (94 loc) · 4.79 KB
/
DataQueueBindings.cpp
File metadata and controls
126 lines (94 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "DataQueueBindings.hpp"
// std
#include <chrono>
// depthai
#include "depthai/device/DataQueue.hpp"
void DataQueueBindings::bind(pybind11::module& m){
using namespace dai;
using namespace std::chrono;
// To prevent blocking whole python interpreter, blocking functions like 'get' and 'send'
// are pooled with a reasonable delay and check for python interrupt signal in between.
// Bind DataOutputQueue
py::class_<DataOutputQueue, std::shared_ptr<DataOutputQueue>>(m, "DataOutputQueue")
.def("getName", &DataOutputQueue::getName)
.def("addCallback", static_cast<int(DataOutputQueue::*)(std::function<void(std::string, std::shared_ptr<ADatatype>)>)>(&DataOutputQueue::addCallback), py::arg("callback"))
.def("addCallback", static_cast<int(DataOutputQueue::*)(std::function<void(std::shared_ptr<ADatatype>)>)>(&DataOutputQueue::addCallback), py::arg("callback"))
.def("addCallback", static_cast<int(DataOutputQueue::*)(std::function<void()>)>(&DataOutputQueue::addCallback), py::arg("callback"))
.def("removeCallback", &DataOutputQueue::removeCallback, py::arg("callbackId"))
.def("setBlocking", &DataOutputQueue::setBlocking, py::arg("blocking"))
.def("getBlocking", &DataOutputQueue::getBlocking)
.def("setMaxSize", &DataOutputQueue::setMaxSize, py::arg("maxSize"))
.def("getMaxSize", &DataOutputQueue::getMaxSize)
.def("getAll", [](DataOutputQueue& obj){
std::vector<std::shared_ptr<ADatatype>> messages;
bool timedout = true;
do {
{
// releases python GIL
py::gil_scoped_release release;
// block for 100ms
messages = obj.getAll(milliseconds(100), timedout);
}
// reacquires python GIL for PyErr_CheckSignals call
// check if interrupt triggered in between
if (PyErr_CheckSignals() != 0) throw py::error_already_set();
} while(timedout); // Keep reiterating until a message is received (not timedout)
return messages;
})
.def("get", [](DataOutputQueue& obj){
std::shared_ptr<ADatatype> d = nullptr;
bool timedout = true;
do {
{
// releases python GIL
py::gil_scoped_release release;
// block for 100ms
d = obj.get(milliseconds(100), timedout);
}
// reacquires python GIL for PyErr_CheckSignals call
// check if interrupt triggered in between
if (PyErr_CheckSignals() != 0) throw py::error_already_set();
} while(timedout);
return d;
})
.def("has", static_cast<bool(DataOutputQueue::*)()>(&DataOutputQueue::has))
.def("tryGet", static_cast<std::shared_ptr<ADatatype>(DataOutputQueue::*)()>(&DataOutputQueue::tryGet))
.def("tryGetAll", static_cast<std::vector<std::shared_ptr<ADatatype>>(DataOutputQueue::*)()>(&DataOutputQueue::tryGetAll))
;
// Bind DataInputQueue
py::class_<DataInputQueue, std::shared_ptr<DataInputQueue>>(m, "DataInputQueue")
.def("getName", &DataInputQueue::getName)
.def("setBlocking", &DataInputQueue::setBlocking, py::arg("blocking"))
.def("getBlocking", &DataInputQueue::getBlocking)
.def("setMaxSize", &DataInputQueue::setMaxSize, py::arg("maxSize"))
.def("getMaxSize", &DataInputQueue::getMaxSize)
.def("send", [](DataInputQueue& obj, std::shared_ptr<ADatatype> d){
bool sent = false;
do {
// block for 100ms
{
// Release GIL, then block
py::gil_scoped_release release;
sent = obj.send(d, milliseconds(100));
}
// reacquires GIL as PyErr_CheckSignals requires GIL
// check if interrupt triggered in between
if (PyErr_CheckSignals() != 0) throw py::error_already_set();
} while(!sent);
})
.def("send", [](DataInputQueue& obj, std::shared_ptr<dai::RawBuffer> d){
bool sent = false;
do {
// block for 100ms
{
// Release GIL, then block
py::gil_scoped_release release;
sent = obj.send(d, milliseconds(100));
}
// reacquires GIL as PyErr_CheckSignals requires GIL
// check if interrupt triggered in between
if (PyErr_CheckSignals() != 0) throw py::error_already_set();
} while(!sent);
})
;
}