How can I have threadsafe instance variables within Inlinescript block when using Powershell workflows? -


i'm using powershell 3 , playing workflows , have run across curious. using inlinescript block hold code represent task want run many instances of. have found variables declared within inlinescript block can accessed across parallel instances.

my sample here has parallel process sleep random number of milliseconds can see console output multiple concurrent processes may sleep same number of milliseconds. sure, possible, it's it's clear data not threadsafe.

workflow invoke-foreachparallel {     param([string[]]$computers)     foreach -parallel($pc in $computers)     {         inlinescript {             "executing on $using:pc"             [int]$i = get-random -min 200 -max 3000             "`t$using:pc - sleeping $i milliseconds."             start-sleep -milliseconds $i             "`tfinished processing on $using:pc"         }     } }    cls invoke-foreachparallel (1..50) 

so, question is: how can ensure each concurrent process working own variable instances?

david wyatt answered both , in detail on thread. http://social.technet.microsoft.com/forums/scriptcenter/en-us/155b5638-5477-4e7c-a154-c6b56f6c3145/how-can-i-have-threadsafe-instance-variables-within-inlinescript-block-when-using-powershell?prof=required

simple answer: when use inlinescript, it's not multi-threaded scenario; it's multi-process. workflow foundation starts new powershell.exe process execute code in inlinescript block each time through (you can see happening in task manager while running script). there no way there thread-safety issues between separate processes (unless have debug programs right , use writeprocessmemory silly reason).

edit: on windows 7 system, number of powershell.exe processes spawned in script seems limited 5. i'm not sure if wf tears down , restarts these processes each execution of inlinescript block, or if treats them pool, reusing them until everything's done. assume latter, performance reasons, don't haven't read ms documentation effect.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -