Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'failing': 0.07; 'lines,': 0.07; 'don': 0.09; 'predecessor': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'api': 0.11; 'python': 0.11; 'def': 0.12; 'thread': 0.14; 'circumvent': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'such,': 0.16; 'thread?': 0.16; 'true:': 0.16; 'attach': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'examples': 0.20; 'help.': 0.21; 'seems': 0.21; '(the': 0.22; 'example': 0.22; 'import': 0.22; 'shell': 0.22; 'header:User-Agent:1': 0.23; 'certainly': 0.24; 'text,': 0.24; 'script': 0.25; '2.0': 0.26; 'shown': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'related': 0.29; "doesn't": 0.30; 'forgot': 0.30; 'see,': 0.30; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; 'embed': 0.31; 'os,': 0.31; 'prints': 0.31; 'another': 0.32; 'received:fr': 0.33; 'skip:# 10': 0.33; 'skip:t 40': 0.33; "i'd": 0.34; 'anybody': 0.35; 'but': 0.35; 'curious': 0.36; 'next': 0.36; 'thanks': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'two': 0.37; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'does': 0.39; 'embedded': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'tell': 0.60; 'hope': 0.61; "you're": 0.61; 'first': 0.61; 'show': 0.63; 'soon': 0.63; 'provide': 0.64; 'more': 0.64; 'different': 0.65; 'due': 0.66; 'details,': 0.68; 'prompt': 0.68; 'link:': 0.72; 'received:109': 0.72; 'dias': 0.84; 'gelonida': 0.84; 'url:reference': 0.84; 'versions)': 0.84; 'carlos': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Gelonida N Subject: Re: embedding ipython kernel in a thread Date: Mon, 09 Jun 2014 16:55:57 +0200 References: <5395B800.3040504@premium-sponsor.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 1-75-190-109.dsl.ovh.fr User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 In-Reply-To: <5395B800.3040504@premium-sponsor.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 86 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1402325785 news.xs4all.nl 2912 [2001:888:2000:d::a6]:42818 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73034 On 6/9/2014 3:34 PM, Carlos Anselmo Dias wrote: > > 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-.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 ... > Not sure what you are trying to tell me with attached PHP code. I hope my script is understandable to those who embedded IPython into their own python application. Some details, that I forgot to mention: I'm using IPython 2.0 (the API is different to the predecessor versions) and I based my code on the examples shown on the IPython web site. However the example code doesn't seem to work in any other thread than the main thread. Related link: http://ipython.org/ipython-doc/2/interactive/reference.html#embedding thanks again for any help. I'm rather curious to learn why the embedded kernel seems to block and top find a way to circumvent this behavoiur.