Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python code pauses execution while waiting for Natlink #228

Closed
LexiconCode opened this issue Apr 10, 2018 · 2 comments
Closed

Python code pauses execution while waiting for Natlink #228

LexiconCode opened this issue Apr 10, 2018 · 2 comments
Labels
Documentation Issues related to Documentation NatLink A catchall label for issues related to NatLink Question Questions regarding utilization of Castor

Comments

@LexiconCode
Copy link
Member

LexiconCode commented Apr 10, 2018

So there's a needed to include some functions that run in the background within _caster.py. Unfortunately the code seems to be paused unless dictation is being processed or toggling DNS on and off.

As a test case a logging function can be used in _caster.py

import threading

def printit():
  threading.Timer(1.0, printit).start()
  print "Hello, World!"
printit()

The logging function repeats every 1 second and outputs the current timestamp. Here is the output when run from the Python at the command line:

20180410_09:31:45 - 
20180410_09:31:46 - 
20180410_09:31:47 -

And here is the output when run as part of Caster:

20180410_09:15:23 - 
20180410_09:18:17 - 
20180410_09:26:58 -

Why is this the case and what would be a recommended workaround?

@LexiconCode LexiconCode added the Question Questions regarding utilization of Castor label Apr 10, 2018
@LexiconCode
Copy link
Member Author

I think this answers part of my question.
(http://qh.antenna.nl/unimacro/implementationandacceptanceofnatlink.pdf)

At this point, you may be wondering where the Windows message loop for the Python macro system is to be
found. User macro files cannot have a Windows message loop (no call to natlink.wairForSpeech) or the macro
system controller would stop execution every time it loaded a user’s macro file.
However, there is also no Windows message loop in the macro system controller code itself (natlinkmain.py).
When natlinkmain is loaded from the initialization of the NatLink compatibility module, it loads all of the user
macro files and then returns control to the compatibility module initialization routine. The compatibility module
initialization code then returns control back to Dragon NaturallySpeaking itself.
Instead, the Windows message loop is inside Dragon NaturallySpeaking. The code in NatSpeak that load
compatibility modules will enter a Windows message loop once all of the specified compatibility modules have
been loaded. Individual compatibility modules do not have to enter a Windows message loop, and they should
not enter a Windows message loop since this might impact the initialization of other compatibility modules (or user
macro files).

@LexiconCode
Copy link
Member Author

Courtesy of R. Wilke on the knowbrainer.com forums. "Here are a few comments on threading from Joel Gould, copied from inside the NatLink source code"

Notes on Python threading: Normally you do not have to worry about threading in Python. Although this subsystem has multiple threads, only one thread is every used in conjunction with Python. However, when used in other systems which both include message loops and support Python threading, we need to cleanly lock and unlock the Python interpreter to support threading. If you pass a TRUE flag to natConnect then we enable Python threading support. When enabled, we unlock the Python whenever we enter a message loop and we make sure that we establish the Python thread state in all COM callbacks.

@LexiconCode LexiconCode added the NatLink A catchall label for issues related to NatLink label Apr 10, 2018
@LexiconCode LexiconCode added the Documentation Issues related to Documentation label Apr 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues related to Documentation NatLink A catchall label for issues related to NatLink Question Questions regarding utilization of Castor
Projects
None yet
Development

No branches or pull requests

1 participant