[IronPython] Importing on another Thread

Jimmy Schementi Jimmy.Schementi at microsoft.com
Wed Jul 2 22:34:07 CEST 2008


Dan Eloff wrote:
> I started the thread like so:
>
> t = Thread(ThreadStart(preload))
> t.Start()
>
> And I used __import__ to load the modules.

Dan, reason this doesn't work is because the XAP API isn't threadsave, so we fail when trying to use it from a background thread. If you take a look at Microsoft.Scripting.Silverlight/BrowserScriptHost.cs, you're hitting the "if (!DynamicApplication.InUIThread) { return null; }" line in TryGetSourceFile. DynamicApplication.DownloadContents eventually calls Application.GetResourceStream, which will throw if on a background thread.

It'd be interesting to see if that API is threadsave, since it isn't UI-bound. To be honest, it might be now ... I'll double check.

Today, we spend a large time in the JIT, and the JIT runs on the same thread, so JITing on the UI thread will actually hang your browser. This is what you're trying to avoid, right? In that case, just warming up the DLR on a background thread might be good enough, so we should add that to Microsoft.Scripting.Silverlight.DynamicApplication.

Anyway, I'll see if we can make "import" work on a background thread.

~Jimmy



More information about the Ironpython-users mailing list