Hi Mark, Andrew,
There are ca.context_create(), ca.context_destroy(),
ca.attach_context() functions that go directly to the C library
functions.
In all (both) examples I had of using put() with threads, a new
context was explicitly created, then PVs were created and used within
that thread, and finally the context was destroyed.
Andrews suggestion of attaching to the "main thread" context works
too. I think there is a mistake in ca.py, as
ca.attach_context(context) raises an exception if the return value
from ca_attach_context() is not ECA_ISATTACHED (I think I read the doc
incorrectly -- I get ECA_NORMAL). I pushed the change to ca.py to run
PySEVCHK expecting expect ECA_NORMAL instead of ECA_ISATTACHED to
github. Ignoring that bug for now, attaching to the existing context
like this does work:
#
import time, epics, threading
def threaded_putwait(pv, context, value):
"put-with-wait for calling in a thread"
t0 = time.time()
try:
epics.ca.attach_context(context)
except:
pass
pv.put(value, wait=True, timeout=10.0)
print ' - threaded_putwait done (%.3f sec)' % (time.time()-t0)
if __name__ == '__main__':
pvname = 'XXX.VAL'
target = 10
pv = epics.PV(pvname)
pv.put(-target, wait=True)
context = epics.ca.current_context()
th = threading.Thread(target=threaded_putwait,
args=(pv, context, target))
th.start()
th.join()
print 'All Done.'
Mark, is that approximately what you found as well?
Perhaps it would be better for a PV to save its context at creation,
and automatically know to switch to its context before doing any work?
That would make the saving and attaching of the context in the above
script unnecessary, so that "plain looking" code could more easily be
made multi-threaded without having to worry about contexts. I'm not
sure what the negative consequences of that would be.
--Matt Newville
- Replies:
- RE: PyEpics and Python threads Vigder, Mark
- References:
- PyEpics and Python threads Vigder, Mark
- Re: PyEpics and Python threads Matt Newville
- Re: PyEpics and Python threads Andrew Johnson
- RE: PyEpics and Python threads Vigder, Mark
- Navigate by Date:
- Prev:
creating Deb/RPM packages (with CMake) Martin Konrad
- Next:
Re: ca.py - Throws error - workaround Luca Luisa
- Index:
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
<2011>
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
- Navigate by Thread:
- Prev:
RE: PyEpics and Python threads Vigder, Mark
- Next:
RE: PyEpics and Python threads Vigder, Mark
- Index:
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
<2011>
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
|