Title: PowerShell class instance methods cannot run concurrently · Issue #3651 · PowerShell/PowerShell · GitHub
Open Graph Title: PowerShell class instance methods cannot run concurrently · Issue #3651 · PowerShell/PowerShell
X Title: PowerShell class instance methods cannot run concurrently · Issue #3651 · PowerShell/PowerShell
Description: This behavior changed between PowerShell versions 5.0 and 5.1. The change was to make PowerShell class method script blocks bound to the runspace in which they were created. If a class instance method is run from within a different runsp...
Open Graph Description: This behavior changed between PowerShell versions 5.0 and 5.1. The change was to make PowerShell class method script blocks bound to the runspace in which they were created. If a class instance met...
X Description: This behavior changed between PowerShell versions 5.0 and 5.1. The change was to make PowerShell class method script blocks bound to the runspace in which they were created. If a class instance met...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/3651
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PowerShell class instance methods cannot run concurrently","articleBody":"This behavior changed between PowerShell versions 5.0 and 5.1. The change was to make PowerShell class method script blocks bound to the runspace in which they were created. If a class instance method is run from within a different runspace then it gets \"marshaled\" back to the original runspace (via engine events). The result is that if a single class instance is shared among multiple runspaces (e.g., a runspace pool) with the intention to execute instance methods concurrently, the methods run serially instead.\r\n\r\nI don't know why this changed between 5.0 and 5.1 but it looks to be by design. @vors can you please comment?\r\n\r\nRepro steps:\r\n```powershell\r\nPS \u003e (measure-command { .\\RunspaceInstanceTest.ps1 }).TotalSeconds\r\n\r\nRunspaceInstanceTest.ps1 file script:\r\nclass Writer\r\n{\r\n static WriteLineS([string] $Msg)\r\n {\r\n for ($i=0; $i -lt 10; $i++)\r\n {\r\n [Console]::WriteLine(\"Static: Loop $i - $Msg\")\r\n Start-Sleep -MilliSeconds 100\r\n }\r\n }\r\n\r\n WriteLineI([string] $msg)\r\n {\r\n for ($i=0; $i -lt 10; $i++)\r\n {\r\n [Console]::WriteLine(\"Instance: Loop $i - $Msg\")\r\n Start-Sleep -MilliSeconds 100\r\n }\r\n }\r\n}\r\n\r\n$script = @'\r\n param ([object] $writerInstance, [string] $Title)\r\n\r\n class Writer\r\n {\r\n static WriteLineS([string] $Msg)\r\n {\r\n for ($i=0; $i -lt 10; $i++)\r\n {\r\n [Console]::WriteLine(\"Static: Loop $i - $Msg\")\r\n Start-Sleep -MilliSeconds 100\r\n }\r\n }\r\n\r\n WriteLineI([string] $msg)\r\n {\r\n for ($i=0; $i -lt 10; $i++)\r\n {\r\n [Console]::WriteLine(\"Instance: Loop $i - $Msg\")\r\n Start-Sleep -MilliSeconds 100\r\n }\r\n }\r\n }\r\n\r\n # Create new instance\r\n #$writerInstance = [Writer]::new()\r\n\r\n # Instance write\r\n $writerInstance.WriteLineI(\"$Title\")\r\n\r\n # Static write\r\n #[Writer]::WriteLineS(\"$Title\")\r\n'@\r\n\r\n$writer = [Writer]::new()\r\n\r\n$rsp = [runspacefactory]::CreateRunspacePool(1, 10, $host)\r\n$rsp.Open()\r\n\r\nclass Task\r\n{\r\n [powershell] $powershell\r\n [System.IAsyncResult] $Async\r\n}\r\n\r\n$tasks = @()\r\n\r\n1..5 | foreach {\r\n\r\n $task = [Task]::new()\r\n $tasks += $task\r\n\r\n $task.powershell = [powershell]::Create()\r\n $task.powershell.RunspacePool = $rsp\r\n $task.Async = $task.powershell.AddScript($script).AddArgument($writer).AddArgument(\"Task $_\").BeginInvoke()\r\n}\r\n\r\nforeach ($task in $tasks)\r\n{\r\n $task.powershell.EndInvoke($task.Async)\r\n $task.powershell.Dispose()\r\n}\r\n\r\n$rsp.Dispose()\r\n\r\n```\r\nExpected:\r\n5 concurrently running 1 second loops (with sleep) should take about 1 second to run.\r\n\r\nActual Result:\r\nIt takes about 5 seconds to run indicating that the concurrent scripts are running serially.","author":{"url":"https://github.com/PaulHigin","@type":"Person","name":"PaulHigin"},"datePublished":"2017-04-26T16:11:50.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/3651/PowerShell/issues/3651"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c3ffc1d7-4164-6c18-5fcf-daec16664c6e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CFE8:1CEF06:1B15CF4:258F193:6A5F36E1 |
| html-safe-nonce | 85e6dda3af5ee9b66ab098bfca5e877d1aa509f80fd8b7a4c8018cd60765e866 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRkU4OjFDRUYwNjoxQjE1Q0Y0OjI1OEYxOTM6NkE1RjM2RTEiLCJ2aXNpdG9yX2lkIjoiMzQ2ODk0MzUwNjQ5MjMwNzE2OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0cbf2320464ad62ddc20601f8e169e8674e0f6e0443d461280eb98619d7ceae1 |
| hovercard-subject-tag | issue:224522300 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/3651/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3db4d1497bded9bd526aa0f22ec3ba99d688f067632c51ed699e48259980f887/PowerShell/PowerShell/issues/3651 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3db4d1497bded9bd526aa0f22ec3ba99d688f067632c51ed699e48259980f887/PowerShell/PowerShell/issues/3651 |
| og:image:alt | This behavior changed between PowerShell versions 5.0 and 5.1. The change was to make PowerShell class method script blocks bound to the runspace in which they were created. If a class instance met... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | PaulHigin |
| hostname | github.com |
| expected-hostname | github.com |
| None | ab2497d9345a59709926b385d88458fa5932c82ec066288448eb68b34cbcbbda |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ba0ac57287507693938b0a2de45402a22cda0785 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width