Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot make scriptblocks as a parameters executes in parallel #12

Open
vors opened this issue Jan 23, 2015 · 3 comments
Open

Cannot make scriptblocks as a parameters executes in parallel #12

vors opened this issue Jan 23, 2015 · 3 comments

Comments

@vors
Copy link
Collaborator

vors commented Jan 23, 2015

If you run this line, it will take more then 3 seconds.

@({sleep 1; 1}, {sleep 1; 2}, {sleep 1; 3}) | Invoke-Parallel { return $_.Invoke() }

One of the things that can be really cool: make Invoke-Parallel work with pester tests. So I can pass describe blocks as an -InputObject

@RamblingCookieMonster
Copy link
Owner

Ahh, cool idea!

Looking at verbose output, the actual runtime is ~ .08 seconds for each. It looks like the hangup is when we call begininvoke():

$temp.Runspace = $powershell.BeginInvoke()

If I step through manually, I can reproduce this.

#Set things up
    $Throttle = 20
    $ScriptBlock = $ExecutionContext.InvokeCommand.NewScriptBlock("param(`$_, `$parameter)`r`n" + {$_.invoke()}.ToString()) 
    $object = {start-sleep -Seconds 10; "boo!"}

    $sessionstate = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
    $runspacepool = [runspacefactory]::CreateRunspacePool(1, $Throttle, $sessionstate, $Host)
    $runspacepool.Open() 

    $powershell = [powershell]::Create()
    [void]$PowerShell.AddScript($ScriptBlock).AddArgument($object).AddArgument($parameter)
    $powershell.RunspacePool = $runspacepool

#I get a delay here...
    $rs = $powershell.BeginInvoke()

@RamblingCookieMonster
Copy link
Owner

This is a bit out of my wheelhouse, but this might help identify the root cause:

Measure-command { @({sleep 5; 1}, {sleep 5; 2}, {sleep 5; 3}) | Invoke-Parallel { [scriptblock]::Create( $_ ).invoke() } }

Returns in expected ~ 5 seconds.

Measure-command { @({sleep 5; 1}, {sleep 5; 2}, {sleep 5; 3}) | Invoke-Parallel {  $x = $_.psobject.copy(); $x.invoke() } }

Returns in unexpected ~ 15 seconds.

@vors
Copy link
Collaborator Author

vors commented Jan 26, 2015

@lzybkr comment:
If you call [ScriptBlock]::Create - the script block doesn't know it's runspace.
If you use { } - then it does, and will always run on that runspace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants