embedding ipython kernel in a thread

Carlos Anselmo Dias carlos at premium-sponsor.com
Mon Jun 9 09:34:56 EDT 2014


On 06/09/2014 01:59 PM, Gelonida N wrote:
> Hi,
>
> I'd like to embed an ipython kernel in an appliction, such, that I can 
> use
>
> ipython console --existing kernel-<pid>.json lateron to connect to it 
> and to look at some values
>
>
> Due to various reason I don not want to start it in the main thread.
>
> If you look at following example you will see, that I can start an 
> ipython kernel and
> connect to it as long as I start it in the main thread.
>
> as soon as I start the kernel in another thread my ipython client 
> prints  the first few lines and a welcome text, but
> no prompt is shown.
>
> Does anybody know a way o how to start IPython outside of the main 
> thread?
>
>
> Below code trying to show a working setup with ipython kernel in the 
> main thread and failing if ipython kernel is started from another thread
>
> #!/usr/bin/env python
> import os, time, threading
> from IPython import embed_kernel
>
> cnt  = { 'value' : 0 }
> def run_ip_kernel():
>     embed_kernel(local_ns={ 'cnt': cnt})
>
> def example_thread():
>     while True:
>         cnt['value'] += 1
>         print(cnt['value'])
>         time.sleep(5)
>
> main_thread = run_ip_kernel
> other_thread = example_thread
>
> # if I uncomment the next two lines, then I can no more
> # execute my interactive IPython shell
> #main_thread = example_thread
> #other_thread = run_ip_kernel
>
> print("now you can try to connect to this shell with:")
> print("ipython console --existing kernel-%d.json" % os.getpid())
> threading.Thread(target=other_thread).start()
> main_thread()
>
>

Whatever you're trying to do(...) ... if you look at the script I 
provide you in attach and if you can think how usefull it would be to 
control exponencially what you need ... that's certainly one best 
approach ...

https://www.youtube.com/watch?v=p7CaiWxKYBo

Regards,
Carlos
-------------- next part --------------
	$id_flux["idsponsor"]=2;
//	$flux_treat=array(397,382,769,770);
	//$flux_treat=array(397,382);
	$flux_treat=array(521,6876);
	$textemailmessage="";
	include("../../conf.inc.php");



if(is_array($flux_treat)){
	foreach($flux_treat as $kfluxtotreat => $vfluxtotreat){
		//echo $kfluxtreatsubtype."<br>";
		
		if(!empty($array_language_flux_default[$vfluxtotreat])){
			$id_language_default=$array_language_flux_default[$vfluxtotreat];
		}
		$info_page_records=explode("|",$array_number_records_per_page_start_flux[$vfluxtotreat]); 
		if(is_array($id_flux["detail"][$vfluxtotreat])){
			foreach($id_flux["detail"][$vfluxtotreat]["Sponsor"]["url"]["information"]["xml"] as $kfluxtreatsubtype=>$vfluxtreatsubtype){
				//control if execution is allowed by kfluxtreatsubtype and add exit
				echo $kfluxtreatsubtype."<br>";
				if(check_lock_control_($kfluxtreatsubtype,"start")=="ok"){
				
						$_ids_types_xml_cache_=$_ids_types_xml_[$kfluxtreatsubtype];
						$_time_of_cache_flux_subtype=$_ids_types_xml_time_cache_[$kfluxtreatsubtype];
						//echo "time of cache ".$_time_of_cache_flux_subtype."<br>";
						
						if($kfluxtreatsubtype =="xml-movies"){
							
							$number_records_or_end_position=$info_page_records[0];
							$counterfilter=0;
							foreach($id_flux["detail"][$vfluxtotreat]["languages"] as $klg => $vlg){
								foreach($ids_cats_sponsor[$vfluxtotreat] as $kcat => $vcat){	
									 $cat_convereted_cache=$vcat;
									 $pagenumb=$limitepaginas=$info_page_records[1];
									  		
										while ($pagenumb<=$limitepaginas){
											$replacements=array($id_flux["idsponsorfk"],$id_flux["loginsponsor"],$id_flux["passwordsponsor"],$kcat,"{idcatsonsponsor}",$pagenumb,$vlg,$number_records_or_end_position,"{filter}",$_SERVER['REMOTE_ADDR'],$id_flux["keysponsor"],"{tracker}","{iditemdetail}","{idsubitemdetail}","{subtracker}");	
											$urlstringtreat =str_replace($patterns, $replacements, $vfluxtreatsubtype);
											$page_number_cache=$pagenumb; 		
											echo "<b>".$urlstringtreat."</b><br>";
											
											
											
											$myXML = open_xml_parsing($generatecache="yes",$urlstringtreat,$id_flux["idsponsor"],$vfluxtotreat,$_ids_types_xml_cache_,$klg,$_id_filter_="x",$cat_convereted_cache,$_id_product_sub_type_="x",$_time_of_cache_flux_subtype,"yes|5;3|".$page_number_cache);
											if(substr($myXML,0,8)=="ERRORMSG"){
												$textemailmessage.=$myXML;
											}else{
												$xml = new CXml();
												$xml->Set_XML_data($myXML);

																
																$global1_[$video->id]['str_informations_db'] = prepare_strings_database(remove_weird_symbols_caracteres(treatment_string_tags($_complete_str_="",$_tag_to_treat_="",$str_array_str_informations_db),$vfluxtotreat));
																unset($str_array_str_informations_db,$str_array_images);
																
																$myMovie=movies_management($global1_);
																if(substr($myMovie,0,8)=="ERRORMSG"){
																	$textemailmessage.=$myMovie;
																	unset($global1_[$video->id]);
																}else{
																	unset($global1_[$video->id]);
																}
															
															}//if(!is_array($global1_[$video->id])){		
														}//foreach($xml->obj_data->videos_site[0]->videos[0]->video as $video){
													}//if(isset($xml->obj_data->videos_site[0]->videos[0])){
											}//else if(substr($myXML,0,8)=="ERRORMSG"){
											$pagenumb++;
											$limitepaginas=$xml->obj_data->videos_site[0]->navigation[0]->nb_pages[0];
											unset($myXML);
											if($pagenumb >$limitepaginas){
												break;
											}					
										}//while ($pagenumb<=$limitepaginas){
										//echo $array_modules_to_include_subtype[$kfluxtreatsubtype]."<br>";	
										//@include($array_modules_to_include_subtype[$kfluxtreatsubtype]);
									/*
									if($vfluxtotreat==382 || $vfluxtotreat==397){							
										$myMovie=movies_management($global1_);
										if(substr($myMovie,0,8)=="ERRORMSG"){
											$textemailmessage.=$myMovie;
											echo $textemailmessage."<br>"; 
										}
										unset($global1_);
									}
									*/
								}//foreach($ids_cats as $kcat => $vcat){	
								/*
								if($vfluxtotreat==382 || $vfluxtotreat==397){						
									$myMovie=movies_management($global1_);
									if(substr($myMovie,0,8)=="ERRORMSG"){
										$textemailmessage.=$myMovie;
										echo $textemailmessage."<br>"; 
									}
									unset($global1_);
								}
								*/
								$counterfilter++;
							}//foreach($id_flux["detail"][$k]["languages"] as $klg => $vlg){				
						}	
				}//if(check_lock_control_($kfluxtreatsubtype)==""){
				check_lock_control_($kfluxtreatsubtype,"end");
			}//foreach($id_flux["detail"][$vfluxtotreat]["Sponsor"]["url"]["information"]["xml"] as $kfluxtreatsubtype=>$vfluxtreatsubtype){
		}//if(is_array($id_flux["detail"])){
	}
}



More information about the Python-list mailing list