I found two ways to do this. Both are undocumented, so all usual caveats apply.
This can be done using
Parallel`Developer`$InitCode. While undocumented, this does appear fairly stable.
First trigger loading the parallel tools:
ReleaseHold[$InitCode] is run on every new subkernel for user initialization.
Parallel`Developer`$InitCode = Hold[your; code; here]
One problem with this is that it will be run only after packages registered by
ParallelNeeds are loaded. Thus it cannot be used to set the
The following is a hack messing with the internals of the parallel tools package. Use at your own risk. The advantage is that it can be used to set
$Path, to be used with
Parallelize; (* trigger loading *)
AddInitCode[code] sends code to running subkernels and uses
AppendInitCode to remember it.
AppendInitCode[code] is for load-time setup of init code.
Register code to be run at kernel startup like this:
Parallel`Client`HoldCompound[your; code; here],
True (* permanent *)
The second argument controls whether this init code should survive
Parallel`Developer`ClearKernels. This argument can be omitted and defaults to
This is the same mechanism that
ParallelNeeds uses. Make sure to register the
$Path change before evaluating
SetSharedFunction also make use of
These initialization code will be registered in the following variables:
$clientOriginalCode contains code set to persist even after
$clientCode contains all init code, both persistent and not.