solidworks_mcp.adapters.com_executor¶
solidworks_mcp.adapters.com_executor ¶
Single-threaded executor for SolidWorks COM calls.
Background: SolidWorks COM is STA (single-threaded apartment). An
IDispatch proxy obtained on thread A cannot be invoked from thread B —
pywin32's late-binding surfaces this as
AttributeError: SldWorks.Application.<method>. FastMCP dispatches async
tool handlers on worker threads that are not the thread where
connect() ran, so any cached swApp reference breaks.
This module provides a single dedicated STA worker thread (ComExecutor).
All COM work is submitted to it as callables and awaited via Future.
Because exactly one thread ever touches COM:
CoInitialize()is called once at thread startup.self.swAppandself.currentModelcan be shared instance attributes without marshalling — no thread-local trickery.- STA constraints are satisfied (SW is happy).
_FlagAsMethodresults accumulate on the same object lifetime.
Usage:
executor = ComExecutor()
executor.start()
try:
result = executor.submit(lambda: sw.ActiveDoc.GetTitle())
finally:
executor.stop()
or with the synchronous helper:
with ComExecutor() as ex:
title = ex.run(lambda: sw.ActiveDoc.GetTitle())
Attributes¶
Classes¶
ComExecutor ¶
Single-threaded STA executor for SolidWorks COM calls.
Thread-safe: submit() / run() may be called from any thread.
Lifecycle
- Construct: creates the executor (no thread yet).
start(): launches the worker thread and waits for it to CoInitialize.submit(fn): schedulesfnon the worker; returns a Future.run(fn): convenience wrapper around submit+result.stop(): signals the worker to exit, joins the thread, then CoUninitializes.
Create the executor (thread not yet running).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Thread name for debugging / logs. |
'SolidWorks-COM'
|
Source code in src/solidworks_mcp/adapters/com_executor.py
Functions¶
run ¶
Run fn on the worker and block until the result is ready.
Convenience wrapper around submit() + Future.result().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[[], T]
|
Zero-argument callable to run on the COM thread. |
required |
timeout
|
float | None
|
Seconds to wait before raising TimeoutError. |
None
|
Returns:
| Type | Description |
|---|---|
T
|
Whatever |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
if |
Source code in src/solidworks_mcp/adapters/com_executor.py
start ¶
Launch the worker thread and wait until it has CoInitialized.
Idempotent: calling start() on a running executor is a no-op.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
Seconds to wait for CoInitialize to complete. If it doesn't fire in time, raises RuntimeError. |
10.0
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
Worker didn't become ready in time. |
Source code in src/solidworks_mcp/adapters/com_executor.py
stop ¶
Signal the worker to exit and wait for it to join.
After stop() returns, no further submit() calls will be
serviced. Idempotent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
Seconds to wait for the worker to exit cleanly before abandoning the join. |
5.0
|
Source code in src/solidworks_mcp/adapters/com_executor.py
submit ¶
Schedule fn to run on the worker thread.
The callable receives no arguments — close over any state needed
via the enclosing scope. The return value (or exception) is
propagated through the returned Future.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[[], T]
|
Zero-argument callable to run on the COM thread. |
required |
Returns:
| Type | Description |
|---|---|
Future[T]
|
Future that will hold the result or exception. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
Executor isn't running. |