Skip to content

Commit

Permalink
Add missing Py.Gil in base PCM (QuantConnect#6414)
Browse files Browse the repository at this point in the history
- Add missing python Py.Gill in base PortfolioConstructionModel. Adding
  unit test reproducing issue, seg fault
  • Loading branch information
Martin-Molinero authored Jun 20, 2022
1 parent bb0c27f commit a42a536
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Algorithm/Portfolio/PortfolioConstructionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,14 @@ protected void SetRebalancingFunc(PyObject rebalance)
{
try
{
// try convert does not work for timespan
timeSpan = rebalance.As<TimeSpan>();
if (timeSpan != default(TimeSpan))
using (Py.GIL())
{
_rebalancingFunc = time => time.Add(timeSpan);
// try convert does not work for timespan
timeSpan = rebalance.As<TimeSpan>();
if (timeSpan != default(TimeSpan))
{
_rebalancingFunc = time => time.Add(timeSpan);
}
}
}
catch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ def RebalanceFunc(dateRules):
Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 10, 2), new Insight[0]));
}

[TestCase(Language.Python, 2)]
[TestCase(Language.Python, 1)]
[TestCase(Language.Python, 0)]
[TestCase(Language.CSharp, 0)]
Expand All @@ -335,6 +336,16 @@ def RebalanceFunc(timeSpan):
return timeSpan").GetAttr("RebalanceFunc");
constructionModel.SetRebalancingFunc(func(TimeSpan.FromMinutes(20)));
}
else if(version == 2)
{
dynamic func = PyModule.FromString("RebalanceFunc",
@"
from datetime import timedelta
def RebalanceFunc(constructionModel):
return constructionModel.SetRebalancingFunc(timedelta(minutes = 20))").GetAttr("RebalanceFunc");
func(constructionModel);
}
else
{
dynamic func = PyModule.FromString("RebalanceFunc",
Expand Down

0 comments on commit a42a536

Please sign in to comment.