Thursday, November 20, 2014

Boost::Python callback triggered from Non-Python created threads

Consider  we have virtual/abstract C++ class that's fully implemented in Python. And for some sake of necessity we have a callback method (e.g. as some sort of event) that is being triggered from different thread on C++ side and is handled in Python .
In such case corresponding callback methods have to manage global interpreter lock state with PyGILState_STATE member variable.
So the resulting C++ callback class definition will look like below (notice that Python method calls are wrapped up with GIL state handling code).


 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
class SessionStatusListenerCallback : public SessionStatusListener
{
public:
 SessionStatusListenerCallback(PyObject *pyObject)
  : self(pyObject) {}

 SessionStatusListenerCallback(PyObject* pyObject, const SessionStatusListener& listener)
  : self(pyObject), SessionStatusListener(listener) {}

 void onSessionStatusChanged(O2GSessionStatus status)
 {
  // GIL state handler
  PyGILState_STATE gstate;
  gstate = PyGILState_Ensure();
  // Python callback
  call_method<void>(self, "onSessionStatusChanged", status);
  // GIL handler release
  PyGILState_Release(gstate);
 }
 
 void onLoginFailed(const char* error)
 {
  // GIL state handler
  PyGILState_STATE gstate;
  gstate = PyGILState_Ensure();
  // Python callback
  call_method<void>(self, "onLoginFailed", error);
  // GIL handler release
  PyGILState_Release(gstate);
 }
private:
 PyObject* const self;
};
 
 

Wednesday, November 12, 2014

Microsoft Visual C++ Compiler for Python 2.7

New tool Microsoft Visual C++ Compiler for Python 2.7 is available. 
I have not tested it yet. But solution for: 
The typical error message you will receive if you need this compiler package is Unable to find vcvarsall.bat. 
 sounds promising :)

Friday, November 7, 2014

Handling "AddressAccessDeniedException: HTTP could not register URL" error

Having self hosting WCF service one can get following exception:

AddressAccessDeniedException: HTTP could not register URL http://+:13025/

By default, listening at a particular HTTP address requires administrator privileges. Since application users does not often have such privileges it's necessary to allow port listening for particular user or group via nesth.

E.g.

netsh http add urlacl url=http://+:13025/applicationsvcs/booking user=DOMAIN\app_account

Proxying WCF service through the Fiddler

Set Fiddler as system wide proxy with preferred port (e.g. 8888).
Change app config of the WCF service host application as follows

<system.net>
  <defaultProxy
            enabled = "true"
            useDefaultCredentials = "true">
   <proxy autoDetect="false" bypassonlocal="false" proxyaddress="http://127.0.0.1:8888"    usesystemdefault="false" />
  </defaultProxy>
</system.net>

That's it.