<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi Tom,<div class=""><br class=""></div><div class="">Thanks for the explanation. I’ll have to think and investigate a bit to see if my thread use is causing some problems elsewhere. Most of our code has been pretty stable, but there are still a few occasional glitches that I haven’t completely understood.</div><div class=""><br class=""></div><div class="">Anyway, session.ui.thread_safe is perfect for resolving the issues we have with adding the surface to the empty session and running commands remotely. The curl solution also works; I had something like this implemented previously for some other purposes, but my own preference is to send commands/data directly rather than writing out a file to be read on the other end.</div><div class=""><br class=""></div><div class="">Thanks again!</div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Albert</div><div class=""><br class=""><div class="">
<meta charset="UTF-8" class=""><div dir="auto" style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div dir="auto" style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div><span style="font-size: 11px;" class="">–––––––––––––––––––––––––––––––––––</span></div><div><span style="font-size: 11px;" class="">Albert Smith-Penzel, Ph.D.</span></div><div><span style="font-style: normal; font-size: 11px;" class="">DFG Project Leader</span></div><div><span style="font-style: normal; font-size: 11px;" class="">Institute for Medical Physics and Biophysics</span></div><div><span style="font-style: normal; font-size: 11px;" class="">Leipzig University</span></div><div><span style="font-style: normal; font-size: 11px;" class="">04107 Leipzig, Germany</span></div><div><span style="font-style: normal; font-size: 11px;" class="">E-mail: </span><a href="mailto:albert.smith-penzel@medizin.uni-leipzig.de" class="">albert.smith-penzel@uni-leipzig.de</a></div><div class=""><br class=""></div></div><br class="Apple-interchange-newline"></div><br class="Apple-interchange-newline"><br class="Apple-interchange-newline">
</div>
<div><br class=""><blockquote type="cite" class=""><div class="">On 4. Jan 2023, at 21:41, Tom Goddard <<a href="mailto:goddard@sonic.net" class="">goddard@sonic.net</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">

<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252" class="">

<div style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
<div style="background-color: rgb(255, 235, 156); width: 100%; border: 1pt solid rgb(156, 101, 0); padding: 2pt; font-size: 10pt; line-height: 12pt; font-family: Calibri; text-align: left;" class="">
<span style="color:#9C6500; font-weight:bold;" class="">WARNUNG:</span> Diese E-Mail kam von außerhalb der Organisation. Klicken Sie nicht auf Links oder Ã¶ffnen Sie keine Anhänge, es sei denn, Sie kennen den Absender und wissen, dass der Inhalt sicher ist.
</div>
<br class="">
<div class="">
<div style="background-color: rgb(255, 235, 156); width: 100%; border: 1pt solid rgb(156, 101, 0); padding: 2pt; font-size: 10pt; line-height: 12pt; font-family: Calibri; text-align: left;" class="">
<span style="color:#9C6500; font-weight:bold;" class="">WARNUNG:</span> Diese E-Mail kam von außerhalb der Organisation. Klicken Sie nicht auf Links oder Ã¶ffnen Sie keine Anhänge, es sei denn, Sie kennen den Absender und wissen, dass der Inhalt sicher ist.
</div>
<br class="">
<div class="">Hi Albert,
<div class=""><br class="">
</div>
<div class="">  ChimeraX is not designed to run arbitrary Python code multithreaded.  There are many reasons.  First the Qt window toolkit is not thread safe -- all Qt GUI calls have to be done in the main thread.  Violating that often results in a crash.  But even
 without the GUI, imagine the main thread is looping over the list of open models while a second thread decides to add a model to that list -- this could break the looping thread many ways.  Python has a global interpreter lock which means it only can run one
 thread at a time.  Still if you pause one thread and then change the state of the ChimeraX session in another thread, the first thread could throw errors in countless ways when it resumes at an arbitrary point in the code with the data structures changed.
  In general very little software is thread safe because it is very difficult to implement.  Usually that is only done by making threads not modify any data structures used by other threads.</div>
<div class=""><br class="">
</div>
<div class="">  Here are two solutions.  1) Have your listener thread run the Python code it receives in the main thread rather than the listener thread.  There is a ChimeraX function that takes a function and queues it to be run in the main thread.</div>
<div class=""><br class="">
</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>session.ui.thread_safe(func, args...)</div>
<div class=""><br class="">
</div>
<div class="">Here is the code if you want to see how it works</div>
<div class=""><br class="">
</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span><a href="https://github.com/RBVI/ChimeraX/blob/2e33222fabb48edec76df6e82a10efedb54a8a25/src/bundles/ui/src/gui.py#L360" class="">https://github.com/RBVI/ChimeraX/blob/2e33222fabb48edec76df6e82a10efedb54a8a25/src/bundles/ui/src/gui.py#L360</a></div>
<div class="">
<div class=""><br class="">
</div>
<div class="">  A second solution is to use the ChimeraX remotecontrol communication for everything.  That mechanism just runs ChimeraX commands, but you can run Python code using it by just sending the command "open script27.py" where you put the Python you want to
 run in a file script27.py.</div>
<div class=""><br class="">
</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>Tom</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
<blockquote type="cite" class="">
<div class="">On Jan 4, 2023, at 4:40 AM, Albert Smith-Penzel via ChimeraX-users <<a href="mailto:chimerax-users@cgl.ucsf.edu" class="">chimerax-users@cgl.ucsf.edu</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
Hello,
<div class=""><br class="">
</div>
<div class="">I am working on a Python project that uses ChimeraX (currently running 1.5 to interactively view dynamics data by mapping it onto a molecule. All the figures/movies in this paper, <a href="https://www.nature.com/articles/s41467-021-27417-y" class="">https://www.nature.com/articles/s41467-021-27417-y</a>,
 were made possible by Chimera! Also thanks to Tom Goddard for explaining to me how some of those figures could be made awhile ago (<a href="https://rbvi.github.io/chimerax-recipes/spherical_harmonics/spherical_harmonics.html" class="">https://rbvi.github.io/chimerax-recipes/spherical_harmonics/spherical_harmonics.html</a> ). </div>
<div class=""><br class="">
</div>
<div class="">So we’re controlling ChimeraX remotely; we launch a session and then can send commands from Python over to Chimera and edit images from within our python code (while still allowing us to interact via the ChimeraX interface). The basic way this works is
 as follows</div>
<div class=""><br class="">
</div>
<div class="">launch chimeraX from our code, including a short python script</div>
<div class="">python script imports our libraries, launches â€œcmxr” (chimeraX remote)</div>
<div class="">cmxr uses multiprocessing.connection.Listener to listen for commands/data coming from Client in our python code</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>!!!The Listener runs in a thread (threading.Thread) so that we continue to be able to interact normally with ChimeraX (launching the Listener without using a thread freezes ChimeraX while waiting
 for commands, rendering it unusable)</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"></span>Then, we just tell it to run some function(s) that we’ve imported into ChimeraX, and provide some data, atom ids, etc. over the Client/Listener connection</div>
<div class=""><br class="">
</div>
<div class="">There are a few things that I can’t figure how to make work via this process. We’ve had a few long-standing workarounds, but I’ve always been a little disturbed that we can’t get this all to work from python alone. At the moment, when we launch ChimeraX,
 we also enable remote control via curl, and some commands are then sent that way (remotecontrol rest start port XXXX)</div>
<div class=""><br class="">
</div>
<div class="">In particular, if I want to send commands that would normally be executed at ChimeraX’s command line, in principle I should be able to use chimerax.core.commands.run. If I execute â€œrun” from a simple python script, everything works, but if I run it from
 within a thread (as required by our Listener setup), then ChimeraX freezes. So, we send these commands via curl on the remotecontrol instead.</div>
<div class=""><br class="">
</div>
<div class="">Second, is there a right way to open models/surfaces from the ChimeraX python shell? At the moment, we also open pdbs via curl commands. For pdbs, maybe I just don’t know the right python command. However, I am re-implementing spherical harmonics referenced
 above. Once I have prepared the surface (“s”), if I add it to session.models, it crashes chimeraX if it is being added within the listener thread, and if there are no models already open. If another model is already open, then it works just fine, and if I
 load the same modules/execute the same commands from within the python shell provided in ChimeraX, it also works (regardless of other models being open). It seems like maybe ChimeraX isn’t able to fully initialize the session from within a thread. </div>
<div class=""><br class="">
</div>
<div class="">I’ve grouped these problems together because I guess mainly they have to do with being unable to do some actions from within threads, and if there’s an explanation/workaround for dealing with that.</div>
<div class=""><br class="">
</div>
<div class="">Thanks for your help!</div>
<div class=""><br class="">
</div>
<div class="">Cheers,</div>
<div class="">Albert</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
<div class="">
<div dir="auto" style="caret-color: rgb(0, 0, 0); letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
<div dir="auto" style="caret-color: rgb(0, 0, 0); letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
<div class=""><span style="font-size: 11px;" class="">–––––––––––––––––––––––––––––––––––</span></div>
<div class=""><span style="font-size: 11px;" class="">Albert Smith-Penzel, Ph.D.</span></div>
<div class=""><span style="font-style: normal; font-size: 11px;" class="">DFG Project Leader</span></div>
<div class=""><span style="font-style: normal; font-size: 11px;" class="">Institute for Medical Physics and Biophysics</span></div>
<div class=""><span style="font-style: normal; font-size: 11px;" class="">Leipzig University</span></div>
<div class=""><span style="font-style: normal; font-size: 11px;" class="">04107 Leipzig, Germany</span></div>
<div class=""><span style="font-style: normal; font-size: 11px;" class="">E-mail: </span><a href="mailto:albert.smith-penzel@medizin.uni-leipzig.de" class="">albert.smith-penzel@uni-leipzig.de</a></div>
<div class=""><br class="">
</div>
</div>
<br class="Apple-interchange-newline">
</div>
<br class="Apple-interchange-newline">
<br class="Apple-interchange-newline">
</div>
<br class="">
</div>
</div>
_______________________________________________<br class="">
ChimeraX-users mailing list<br class="">
<a href="mailto:ChimeraX-users@cgl.ucsf.edu" class="">ChimeraX-users@cgl.ucsf.edu</a><br class="">
Manage subscription:<br class="">
<a href="https://www.rbvi.ucsf.edu/mailman/listinfo/chimerax-users" class="">https://www.rbvi.ucsf.edu/mailman/listinfo/chimerax-users</a><br class="">
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
</div>
</div>

</div></blockquote></div><br class=""></div></body></html>