For gurus: again on threading
#1
in my script, i need to avoid that, during the onaction and oncontrol execution, the other threads have access to some resources.
so i decided to use a semaphore to sync the different operations.
i wrote something like this:

Quote:class myclass(xbmcgui.windowdialog):
def initializesemaphore(self):
self.semapore = threading.semaphore(1)

def onaction(self, action):
xbmc.log("onaction - before acquire " + threading.currentthread())
self.semaphore.acquire(true)
xbmc.log("onaction - after acquire " + threading.currentthread())

... do something...

xbmc.log("onaction - before release " + threading.currentthread())
self.semaphore.release()
xbmc.log("onaction - after release " + threading.currentthread())

def oncontrol(self, control):
xbmc.log("oncontrol - before acquire " + threading.currentthread())
self.semaphore.acquire(true)
xbmc.log("oncontrol - after acquire " + threading.currentthread())

... do something...

xbmc.log("oncontrol - before release " + threading.currentthread())
self.semaphore.release()
xbmc.log("oncontrol - after release " + threading.currentthread())

def myfunction(self): # this is called by another thread
xbmc.log("myfunction - before acquire " + threading.currentthread())
self.semaphore.acquire(true)
xbmc.log("myfunction - after acquire " + threading.currentthread())

... do something...

xbmc.log("myfunction - before release " + threading.currentthread())
self.semaphore.release()
xbmc.log("myfunction - after release " + threading.currentthread())

what i expect is that, during the execution of onaction or oncontrol, myfunction, if called, will wait to acquire the semaphore before executing.

i navigate the interface, press some buttons and here is the result:

09-03-2006 23:22:46 fatal oncontrol - before acquire <_mainthread(mainthread, started)>
09-03-2006 23:22:46 fatal oncontrol - after acquire <_mainthread(mainthread, started)>
09-03-2006 23:22:46 fatal oncontrol - before release <_mainthread(mainthread, started)>
09-03-2006 23:22:46 fatal oncontrol - after release <_mainthread(mainthread, started)>

09-03-2006 23:22:46 fatal onaction - before acquire <_mainthread(mainthread, started)>
09-03-2006 23:22:46 fatal onaction - after acquire <_mainthread(mainthread, started)>
09-03-2006 23:22:46 fatal onaction - before release <_mainthread(mainthread, started)>
09-03-2006 23:22:46 fatal onaction - after release <_mainthread(mainthread, started)>

09-03-2006 23:22:51 fatal mytest - before acquire <cepgshortdescriptioncontrol(thread-3, started)>
09-03-2006 23:22:51 fatal mytest - after acquire <cepgshortdescriptioncontrol(thread-3, started)>
09-03-2006 23:22:51 fatal mytest - before release<cepgshortdescriptioncontrol(thread-3, started)>
09-03-2006 23:22:51 fatal mytest - after release<cepgshortdescriptioncontrol(thread-3, started)>

09-03-2006 23:23:44 fatal oncontrol - before acquire <_mainthread(mainthread, started)>
09-03-2006 23:23:44 fatal oncontrol - after acquire <_mainthread(mainthread, started)>
09-03-2006 23:23:44 fatal oncontrol - before release <_mainthread(mainthread, started)>
09-03-2006 23:23:44 fatal oncontrol - after release <_mainthread(mainthread, started)>

09-03-2006 23:23:44 fatal onaction - before acquire <_mainthread(mainthread, started)>
09-03-2006 23:23:44 fatal onaction - after acquire <_mainthread(mainthread, started)>
09-03-2006 23:23:44 fatal onaction - before release <_mainthread(mainthread, started)>
09-03-2006 23:23:44 fatal onaction - after release <_mainthread(mainthread, started)>

09-03-2006 23:23:44 fatal oncontrol - before acquire <_mainthread(mainthread, started)>
09-03-2006 23:23:44 fatal onaction - before acquire <_mainthread(mainthread, started)>

09-03-2006 23:23:44 fatal onaction - after acquire <_mainthread(mainthread, started)>
09-03-2006 23:23:44 fatal onaction - before release <_mainthread(mainthread, started)>
09-03-2006 23:23:44 fatal onaction - after release <_mainthread(mainthread, started)>
09-03-2006 23:23:44 fatal oncontrol - after acquire <_mainthread(mainthread, started)>
09-03-2006 23:23:44 fatal oncontrol - before release <_mainthread(mainthread, started)>
09-03-2006 23:23:44 fatal oncontrol - after release <_mainthread(mainthread, started)>

look at the "bold" lines. the oncontrol function started, printed the first log line and then it was interrupted by the onaction function which acquired the semaphore.
this could happen with different threads but they are running in the same thread (i.e. _mainthread(mainthread, started)).

now, i really don't know what is happening.

is anybody able to explain me?

thx.
Reply
#2
self.semapore = threading.semaphore(1)

was this a copy and paste?
For python coding questions first see http://mirrors.xbmc.org/docs/python-docs/
Reply
#3
(nuka1195 @ mar. 09 2006,23:53 Wrote:self.semapore = threading.semaphore(1)

was this a copy and paste?
you're right. Smile
no, it wasn't copy and paste. the correct code is:

self.semaphore = threading.semaphore(1)
Reply

Logout Mark Read Team Forum Stats Members Help
For gurus: again on threading0