示例#1
0
        /// <summary>
        /// Starts a kernel on this host in a separate process.
        /// If random ports(port= 0) are being used, this method must be called
        /// before the channels are created.
        /// </summary>
        public void StartKernel(Dictionary <string, List <string> > kw = null)
        {
            if (string.Equals(ConnectionInformation.Transport, KernelConnection.TCP_TRANSPORT) &&
                !ConnectionInformation.IsLocalIp())
            {
                throw new Exception(string.Format("Can only launch a kernel on a local interface.  This one is not: {0}.  Make sure that the '*_address' attributes are configured properly.",
                                                  ConnectionInformation.IpAddress));
            }

            ConnectionInformation.Key             = HashHelper.NewIdBytes(false, HASH_KEY_LENGTH);
            ConnectionInformation.KernelName      = this.Spec.DisplayName;
            ConnectionInformation.SignatureScheme = SignatureScheme.HmacSha256;
            ConnectionInformation.WriteConnectionFile();

            // Save args for use in restart
            LaunchArgs = (kw == null) ? new Dictionary <string, List <string> >() : new Dictionary <string, List <string> >(kw);

            // Build the Popen cmd
            List <string> extraArguments = null;

            if (kw != null && kw.ContainsKey(EXTRA_ARGUMENTS))
            {
                extraArguments = kw[EXTRA_ARGUMENTS];
            }

            var kernelCmd  = FormatKernelCmd(extraArguments);
            var envVarDict = Environment.GetEnvironmentVariables();
            IDictionary <string, string> env = new Dictionary <string, string>();

            foreach (var ev in envVarDict.OfType <DictionaryEntry>())
            {
                env.Add((string)ev.Key, (string)ev.Value);
            }
            // Don't allow PYTHONEXECUTABLE to be passed to kernel process.
            // If set, it can bork all the things.
            env.Remove("PYTHONEXECUTABLE");
            if (KernelCmd == null || KernelCmd.Count == 0)
            {
                // If kernel_cmd has been set manually, don't refer to a kernel spec
                // Environment variables from kernel spec are added to os.environ
                env = MergeDicts(env, Spec.Environment);
            }
            else if (ExtraEnvironment != null)
            {
                env = MergeDicts(env, ExtraEnvironment);
            }

            Kernel = LaunchKernel(kernelCmd, env, kw);

            this.ClientSession  = new Session(ConnectionInformation.Key);
            this.ChannelFactory = this.ChannelFactory ?? new ZMQChannelFactory(ConnectionInformation, ClientSession, Logger);
            CreateControlChannel();
        }