<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: Threads</title>
	<atom:link href="http://winpdb.org/docs/threads/feed/" rel="self" type="application/rss+xml" />
	<link>http://winpdb.org</link>
	<description></description>
	<pubDate>Sat, 31 Jul 2010 04:36:15 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: nir</title>
		<link>http://winpdb.org/docs/threads/comment-page-1/#comment-88255</link>
		<dc:creator>nir</dc:creator>
		<pubDate>Thu, 06 Aug 2009 15:06:41 +0000</pubDate>
		<guid isPermaLink="false">http://winpdb.org/?page_id=11#comment-88255</guid>
		<description>If you are interested in a "proper" way of doing this, I would recommend adding a named parameter to start_embedded_debbuger(). For example something like:

&lt;pre&gt;
g_thread_get_ident = thread.get_ident

def start_embedded_debugger(..., get_ident = thread.get_ident):
    global g_thread_get_ident
    g_thread_get_ident = get_ident
    ...
&lt;/pre&gt;

Then replace all (~10) calls to thread.get_ident with g_thread_get_ident. If this done properly and well tested, it may be useful as a patch to Winpdb.</description>
		<content:encoded><![CDATA[<p>If you are interested in a &#8220;proper&#8221; way of doing this, I would recommend adding a named parameter to start_embedded_debbuger(). For example something like:</p>
<pre>
g_thread_get_ident = thread.get_ident

def start_embedded_debugger(..., get_ident = thread.get_ident):
    global g_thread_get_ident
    g_thread_get_ident = get_ident
    ...
</pre>
<p>Then replace all (~10) calls to thread.get_ident with g_thread_get_ident. If this done properly and well tested, it may be useful as a patch to Winpdb.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James A</title>
		<link>http://winpdb.org/docs/threads/comment-page-1/#comment-88241</link>
		<dc:creator>James A</dc:creator>
		<pubDate>Thu, 06 Aug 2009 11:28:24 +0000</pubDate>
		<guid isPermaLink="false">http://winpdb.org/?page_id=11#comment-88241</guid>
		<description>Hi Nir, thanks for repl.

  Yes, our C   code does create a thread state structure for each thread and does use the Python GIL properly.  But Winpdb uses thread.get_ident() to detect, display and switch between threads.  And thread.get_ident() returns the same value for all of my C   threads, even though they have separate thread states.  So without my hack, Winpdb will only debug the first thread in which I have called rpdb2.settrace().  It ignores the others.  Apart from that limitation, there is no functional bug.</description>
		<content:encoded><![CDATA[<p>Hi Nir, thanks for repl.</p>
<p>  Yes, our C   code does create a thread state structure for each thread and does use the Python GIL properly.  But Winpdb uses thread.get_ident() to detect, display and switch between threads.  And thread.get_ident() returns the same value for all of my C   threads, even though they have separate thread states.  So without my hack, Winpdb will only debug the first thread in which I have called rpdb2.settrace().  It ignores the others.  Apart from that limitation, there is no functional bug.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nir</title>
		<link>http://winpdb.org/docs/threads/comment-page-1/#comment-88214</link>
		<dc:creator>nir</dc:creator>
		<pubDate>Thu, 06 Aug 2009 06:24:00 +0000</pubDate>
		<guid isPermaLink="false">http://winpdb.org/?page_id=11#comment-88214</guid>
		<description>Hi James,

I do not expect to break your modification in the near future.

What is the situation now? 
Can Winpdb switch between your C threads?

Do you conform to the following documentation regarding C threads?
http://docs.python.org/c-api/init.html

"It is important to note that when threads are created from C, they don’t have the global interpreter lock, nor is there a thread state data structure for them. Such threads must bootstrap themselves into existence, by first creating a thread state data structure, then acquiring the lock, and finally storing their thread state pointer, before they can start using the Python/C API. When they are done, they should reset the thread state pointer, release the lock, and finally free their thread state data structure."

If so, once you create the thread state, is it still missing a Python thread identifier?</description>
		<content:encoded><![CDATA[<p>Hi James,</p>
<p>I do not expect to break your modification in the near future.</p>
<p>What is the situation now?<br />
Can Winpdb switch between your C threads?</p>
<p>Do you conform to the following documentation regarding C threads?<br />
<a href="http://docs.python.org/c-api/init.html" rel="nofollow">http://docs.python.org/c-api/init.html</a></p>
<p>&#8220;It is important to note that when threads are created from C, they don’t have the global interpreter lock, nor is there a thread state data structure for them. Such threads must bootstrap themselves into existence, by first creating a thread state data structure, then acquiring the lock, and finally storing their thread state pointer, before they can start using the Python/C API. When they are done, they should reset the thread state pointer, release the lock, and finally free their thread state data structure.&#8221;</p>
<p>If so, once you create the thread state, is it still missing a Python thread identifier?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James A</title>
		<link>http://winpdb.org/docs/threads/comment-page-1/#comment-88164</link>
		<dc:creator>James A</dc:creator>
		<pubDate>Wed, 05 Aug 2009 18:38:11 +0000</pubDate>
		<guid isPermaLink="false">http://winpdb.org/?page_id=11#comment-88164</guid>
		<description>I just re-read my comment and it sounds like a "please debug my personal problem" text.

Sorry for that.  The idea was to discuss more generally Winpdb's support for other threading possibilities than only "thread" and "threading".  Using my project only as an example.

It would be great if there were an "official" way to instrument a thread package to make it visible to Winpdb, or maybe better, to derive something from Winpdb that understands my threads.</description>
		<content:encoded><![CDATA[<p>I just re-read my comment and it sounds like a &#8220;please debug my personal problem&#8221; text.</p>
<p>Sorry for that.  The idea was to discuss more generally Winpdb&#8217;s support for other threading possibilities than only &#8220;thread&#8221; and &#8220;threading&#8221;.  Using my project only as an example.</p>
<p>It would be great if there were an &#8220;official&#8221; way to instrument a thread package to make it visible to Winpdb, or maybe better, to derive something from Winpdb that understands my threads.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James A</title>
		<link>http://winpdb.org/docs/threads/comment-page-1/#comment-88143</link>
		<dc:creator>James A</dc:creator>
		<pubDate>Wed, 05 Aug 2009 13:27:37 +0000</pubDate>
		<guid isPermaLink="false">http://winpdb.org/?page_id=11#comment-88143</guid>
		<description>Thanks for a nice debugger.

We are using Python embedded in a C   application and we use C   threads not Python threads (we don't use the thread or threading modules).  Instead the multi-threaded C   can call into Python code and Python code can yield.  Threading is non-preemptive.

When I first tried debugging this with Winpdb, it worked functionally OK except that Winpdb could not list the threads or switch between them.  It was just able to see the context of the thread currently waiting at a breakpoint.

After a bit of hacking I was able to improve the situation.  I replaced the get_ident() method of Python's thread module (also the reference to it in threading) with a new method that detects my C  -created threads and returns their identifier.  I've included the function below.  I also replaced threading._DummyThread in order to provide the right thread names to Winpdb.

My question is whether this is good practice or a nasty hack.  Are you planning to provide more support for this sort of thing in the future?  Is it likely that future versions of Winpdb will break my hack?

Many thanks.


import rpdb2

# set of Python threads which are base-threads for C   thread creation
# filled in during thread creation
# (this enables us to avoid changing the ids of threads used by Winpdb)
sc_elab_threads = set()

def get_ident():
  "replacement function for thread.get_ident() which is gs-aware"
  tmp = orig_get_ident()
  if tmp in sc_elab_threads:
    # this gets the ID of the C   thread
    return gs.gsp_sc_get_curr_process_handle()
  else:
    return tmp

# replace things in thread and threading modules
# note that threading tries to keep a reference to original thread.get_ident()
orig_get_ident = rpdb2.thread.get_ident
rpdb2.thread.get_ident = get_ident
rpdb2.threading._get_ident = get_ident</description>
		<content:encoded><![CDATA[<p>Thanks for a nice debugger.</p>
<p>We are using Python embedded in a C   application and we use C   threads not Python threads (we don&#8217;t use the thread or threading modules).  Instead the multi-threaded C   can call into Python code and Python code can yield.  Threading is non-preemptive.</p>
<p>When I first tried debugging this with Winpdb, it worked functionally OK except that Winpdb could not list the threads or switch between them.  It was just able to see the context of the thread currently waiting at a breakpoint.</p>
<p>After a bit of hacking I was able to improve the situation.  I replaced the get_ident() method of Python&#8217;s thread module (also the reference to it in threading) with a new method that detects my C  -created threads and returns their identifier.  I&#8217;ve included the function below.  I also replaced threading._DummyThread in order to provide the right thread names to Winpdb.</p>
<p>My question is whether this is good practice or a nasty hack.  Are you planning to provide more support for this sort of thing in the future?  Is it likely that future versions of Winpdb will break my hack?</p>
<p>Many thanks.</p>
<p>import rpdb2</p>
<p># set of Python threads which are base-threads for C   thread creation<br />
# filled in during thread creation<br />
# (this enables us to avoid changing the ids of threads used by Winpdb)<br />
sc_elab_threads = set()</p>
<p>def get_ident():<br />
  &#8220;replacement function for thread.get_ident() which is gs-aware&#8221;<br />
  tmp = orig_get_ident()<br />
  if tmp in sc_elab_threads:<br />
    # this gets the ID of the C   thread<br />
    return gs.gsp_sc_get_curr_process_handle()<br />
  else:<br />
    return tmp</p>
<p># replace things in thread and threading modules<br />
# note that threading tries to keep a reference to original thread.get_ident()<br />
orig_get_ident = rpdb2.thread.get_ident<br />
rpdb2.thread.get_ident = get_ident<br />
rpdb2.threading._get_ident = get_ident</p>
]]></content:encoded>
	</item>
</channel>
</rss>
