<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; color: rgb(0, 0, 0); font-size: 14px; font-family: Calibri, sans-serif; ">
<div>
<div>Your threading code isn't as correct or robust as you think it is, if you are getting service startup timeout errors. Windows assumes that the service control callbacks will be executed quickly without being blocked. Typically, the Start callback would
 be creating the service's main work thread, starting it (without waiting on anything from it), and then immediately returning to the SCM. This should generally be an activity that takes up to perhaps a second at most, but the SCM gives you a wide grace period
 - you should never be coming close to that timeout unless something really serious is wrong. Similarly, the Stop and Pause/Resume callbacks would typically use thread synchronization objects (Manual/Auto Reset Events are very common) to communicate the requested
 operation to the main work thread without blocking. Of course, this all means that your main work thread needs to be extremely robust and needs to regularly check for those event signals (and discontinue work with minimal latency) - which is not entirely trivial
 unless you have a fair amount [successful] of experience with it. SCM timeout errors  are indicating an insufficient service design.</div>
<div><br>
</div>
<div>Depending on what your scripts are doing have you considered just using Process.Start() to invoke them, or to invoke a small managed executable that wraps the script execution? There is more overhead with this approach but it is significantly less hassle
 (and more robust) than dealing with a service threading model if you aren't experienced in such things.</div>
<div><br>
</div>
<div>
<p class="MsoNormal" style="font-size: 12pt; font-family: 'Times New Roman', serif; ">
<br>
</p>
<p class="MsoNormal" style="font-size: 12pt; font-family: 'Times New Roman', serif; ">
</p>
<p class="MsoNormal" style="font-size: 14px; "><b><span style="font-size: 14pt; color: rgb(31, 73, 125); ">Keith Rome</span></b><o:p></o:p></p>
<p class="MsoNormal" style="font-size: 14px; "><b><span style="font-size: 10pt; color: rgb(31, 73, 125); ">Principal Architect @ Wintellect (<a href="http://www.wintellect.com" style="color: purple; ">www.wintellect.com</a>)</span></b></p>
<p class="MsoNormal" style="font-size: 14px; "><span style="color: rgb(31, 73, 125); font-size: 10pt; ">770.617.4016 | </span><a href="mailto:rome@wintellect.com" style="color: purple; font-size: 10pt; ">krome@wintellect.com</a></p>
<p class="MsoNormal" style="font-size: 12pt; "><font face="Calibri" style="font-size: 13px; "><br>
</font></p>
<p class="MsoNormal" style="font-size: 12pt; "><font face="Calibri" style="font-size: 13px; "><img src="cid:DB4B3CED-8E4A-4404-A5B0-2853E1AA014D" type="image/png"> </font></p>
<p class="MsoNormal" style="font-size: 12pt; "><font face="Calibri" color="#1c4072" style="font-size: 13px; "><a href="https://www.wintellectnow.com/Account/Promo/ROME-13" style="color: purple; ">Register today</a> for access to our high-quality on-demand training
 resources!</font></p>
<p class="MsoNormal" style="font-size: 12pt; "></p>
<p class="MsoNormal" style="font-size: 12pt; "><font face="Calibri" color="#1c4072" style="font-size: 13px; ">Use promotion code <b>ROME-13</b> to activate your 14-day free trial.</font></p>
<p></p>
</div>
</div>
<div><br>
</div>
<span id="OLK_SRC_BODY_SECTION">
<div style="font-family:Calibri; font-size:11pt; text-align:left; color:black; BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; BORDER-TOP: #b5c4df 1pt solid; BORDER-RIGHT: medium none; PADDING-TOP: 3pt">
<span style="font-weight:bold">From: </span>Eirik Årdal <<a href="mailto:eirik.ardal@bouvet.no">eirik.ardal@bouvet.no</a>><br>
<span style="font-weight:bold">Date: </span>Friday, October 18, 2013 4:20 AM<br>
<span style="font-weight:bold">To: </span>"<a href="mailto:ironpython-users@python.org">ironpython-users@python.org</a>" <<a href="mailto:ironpython-users@python.org">ironpython-users@python.org</a>><br>
<span style="font-weight:bold">Subject: </span>[Ironpython-users] Issues with multithreading, ironpython and numpy<br>
</div>
<div><br>
</div>
<div xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<div lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">First, let me start by saying that I don’t have a lot of experience with Iron Python, or Python for that matter, but I know a person involved with the current project who is an experienced Python developer, but is unable to help with this
 issue since we believe it has to do with our integration between my C# code and his Python code. Also, I am aware that this might not be directly a IronPython issue, but it’s certainly involving IronPython, NymPy for .net and C# and integration between them.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The short version of our problem is that when I call his Python scripts through IronPython in a multithreaded context, I get a System.InsufficientMemoryException thrown by NumPyDotNet. However, if I run my code synchronously on one thread,
 it works fine.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The long version is that I have a Windows Service that constantly checks in a database for new entries that will trigger work to be done (calculating data). I need to be able to do the calculation work on another thread in order to not
 lock up the service, making it unresponsive. But calling the Python scripts through IronPython on my worker thread results in the exception above. If I run my code on one thread, the service is unresponsive during start-up and windows terminates it after a
 certain time, saying it could not put it into its “started” state since its locked by work.
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I have tried various ways to do my work asynchronous, like starting a Task and using the timers in Microsoft’s libraries. I have not tried async/await from .net 4.5 since upgrading to .net 4.5 is not desireable by the project management.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The same problem was explained (not by me) in this stackoverflow post
<a href="http://stackoverflow.com/questions/10003344/ironpython-version-of-numpy-doesnt-work-in-multi-threaded-environment">
http://stackoverflow.com/questions/10003344/ironpython-version-of-numpy-doesnt-work-in-multi-threaded-environment</a> but there are very few other similar issues when I search forums.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Any suggestions or tips will be greatly appreciated.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><b><span style="font-family: Arial, sans-serif; ">Eirik Årdal
<span style="color:#FF0A00">/ </span></span></b><span style="font-family: Arial, sans-serif; ">54°24'42" S 3°25'33" E<span style="color:#FF0A00"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span lang="NO-BOK" style="font-family: Arial, sans-serif; ">Konsulent<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="NO-BOK" style="font-size: 9pt; font-family: Arial, sans-serif; color: gray; ">Bouvet Norge AS, Solheimsgaten 15, NO-5058 Bergen<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="NO-BOK" style="font-size: 9pt; font-family: Arial, sans-serif; color: gray; ">Mobil +47 992 999 31<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="NO-BOK" style="font-size: 10pt; font-family: Arial, sans-serif; color: gray; "><a href="http://www.bouvet.no/?utm_source=Signatur%2Bno&utm_medium=Epost&utm_campaign=Bouvet%2Bsignatur"><span style="color:blue">http://www.bouvet.no</span></a>
   <a href="mailto:eirik.ardal@bouvet.no"><span style="color:blue">eirik.ardal@bouvet.no</span></a></span><span lang="NO-BOK" style="font-size: 12pt; font-family: Arial, sans-serif; "><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="NO-BOK" style="mso-fareast-language:NO-BOK"><o:p> </o:p></span></p>
<p class="MsoNormal"><img border="0" width="124" height="32" id="Picture_x0020_1" src="cid:image003.jpg@01CECBEB.B5A42670" alt="bouvetlogomini"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</span>
</body>
</html>