Open Enteleform opened 7 years ago
This is something that I broke by removing the sub-interpreter code when trying to fix the original version. I was able to get it to run, but with severely limited functionality - i.e., with only one plugin.
Looking back, I suspect the correct way to deal with this is to spawn a new process for each Python interpreter. Python is really bad at handling multiple interpreters in the same process. I am just not familiar enough with multiprocessing in C++ to be able to do that right off the bat.
I'll add this to my rainy-day projects list, but if someone else wants to take a crack at it first, feel free to submit a pull request!
@glitchassassin
Any idea what the performance would be like with multiple interpreters? (around 10-100ish)
I set up a small framework last night based on using a single instance, but then it occurred to me that I could also subclass it for some minimal single-purpose measures
(which is when I ran into the issue).
This setup's intention is to outsource all data-work to Python, and allow a single line point of usage (python_utils.execute
) to retreive data in Lua. This is acheived with a string
property which is paired to GetString
, an output
decorator which sets string
, and Bang("!CommandMeasure"...)
passing an output
-wrapped function string to be run by exec
@ ExecuteBang
.
### StdLib ###
import subprocess
from functools import wraps
SUBLIME_TEXT = "C:/Program Files/Sublime Text/sublime_text.exe"
class Measure:
string = ""
####### Rainmeter Functions ################################################################################
def Reload(self, rainmeter, maxValue):
self.rainmeter = rainmeter
def ExecuteBang(self, command):
try:
exec(f"self.{command}")
except Exception as e:
self.notify(e)
def GetString(self):
return self.string
def Update(self):
pass
def Finalize(self):
pass
####### Output Functions ###################################################################################
@output
def get_CurrentTime(self):
return datetime.now().strftime("%I:%M:%S %p")
####### Utils ##############################################################################################
def log(self, message, ):
self.rainmeter.RmLog(self.rainmeter.LOG_ERROR, str(message))
def notify(self, message):
subprocess.Popen([SUBLIME_TEXT, "--new-window", "--command", f"insert {{\"characters\": \"{message}\"}}"])
####### Output Decorator ###################################################################################
def output(function):
@wraps(function)
def wrapped(self, *args, **kwargs):
result = function(self, *args, **kwargs)
self.string = str(result)
return wrapped
--------- Example Usage ------------------------------------------------------------------------------------
function Update()
time = python_utils.execute{script="PyMeasure", command="get_CurrentTime()"}
meter_utils.set_Text{meter="CurrentTime", text=time}
end
--------- Utils --------------------------------------------------------------------------------------------
python_utils = {}
function python_utils.execute(ARGS) -- script, command
measure = SKIN:GetMeasure(ARGS.script)
SKIN:Bang("!CommandMeasure", ARGS.script, ARGS.command)
return measure:GetStringValue()
end
meter_utils = {}
function meter_utils.set_Text(ARGS) -- meter, text
SKIN:Bang("!SetOption", ARGS.meter, "Text", ARGS.text)
end
This setup builds upon the single instance setup by subclassing Measure
, and allows concise python implementations that can be baked into INI files without the need for Lua.
### Skin Framework ###
from measure import Measure, output
### StdLib ###
from datetime import datetime
class CurrentTime(Measure):
@output
def Update(self):
return datetime.now().strftime("%I:%M:%S %p")
I just tested this plugin out successfully with a single
measure
+meter
combo. However, Rainmeter crashes immediately without logging any errors when I try to create a second set (separatemeasure
+meter
).Both python-script instances are essentially identical aside from the file & class names, which are unique & properly isolated.
Here's a simplified version of the
.ini
file:And here is the simplified code for
Test_A.py
&Test_B.py
: