473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

RE: I want to release the GIL

Piotr Sobolewski wrote:
Hello,
I have such program:

import time
import thread
def f():
* * global lock
* * while True:
* * * * lock.acquire()
* * * * print thread.get_ident()
* * * * time.sleep(1)
* * * * lock.release()
lock=thread.allocate_lock()
thread.start_new_thread(f,())
thread.start_new_thread(f,())
time.sleep(60)
1. You should use the threading module.

2. No need for the "global lock" statement here - you're not rebinding the name "lock".

3. These aren't daemon threads, so your program will never exit. You will need to set a flag or something after the time.sleep(60).
As you can see, I start two threads. Each one works in an infinite
loop.
Inside that loop it acquires lock, prints its own id, sleeps a bit and
then
releases lock.
4. Because you are holding the lock while sleeping, the other thread does not get the chance to run. Sleeping does not release any locks held.

Instead try something like:

while True:
with lock:
print thread.get_ident()
time.sleep(1)

Note that to use the "with lock:" idiom, you need to be using Python 2.6, or Python 2.5 with a "from __future__ import with_statement".

Tim Delaney
Oct 21 '08 #1
6 2756
Thanks for answers.
But what about my main question? Is it possible to release GIL without
sleeping? I know that in this example situation I can achieve my goals
without that - I can just move sleep outside of locked block. But I
just want to know it for future - can I just do something like
thread.gil_release()?
Oct 21 '08 #2
See yield() statement.
Oct 21 '08 #3
Piotr Sobolewski schrieb:
Thanks for answers.
But what about my main question? Is it possible to release GIL without
sleeping? I know that in this example situation I can achieve my goals
without that - I can just move sleep outside of locked block. But I
just want to know it for future - can I just do something like
thread.gil_release()?

No, you can't. That is the reason for the python multiprocessing module
http://docs.python.org/library/multiprocessing.html, which is part of
2.6 standard lib.

For older python versions, there is "processing" that essentially does
the same.

Diez
Oct 21 '08 #4
En Tue, 21 Oct 2008 04:58:00 -0200, Piotr Sobolewski
<pi**************@o2.plescribió:
But what about my main question? Is it possible to release GIL without
sleeping? I know that in this example situation I can achieve my goals
without that - I can just move sleep outside of locked block. But I
just want to know it for future - can I just do something like
thread.gil_release()?
No, you can't release the GIL *and* continue executing Python code.

--
Gabriel Genellina

Oct 21 '08 #5
On Oct 21, 5:09*am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Tue, 21 Oct 2008 04:58:00 -0200, Piotr Sobolewski *
<piotr_sobolew...@o2.plescribió:
But what about my main question? Is it possible to release GIL without
sleeping? I know that in this example situation I can achieve my goals
without that - I can just move sleep outside of locked block. But I
just want to know it for future - can I just do something like
thread.gil_release()?

No, you can't release the GIL *and* continue executing Python code.
I don't think that's what the OP was asking about; I think he merely
wanted to force a GIL release to give the other thread a chance to
run.

Reason being: in his code the lock was re-acquired right after it was
released. This meant that the same thread that released the lock
almost always acquired it right back, since there was only a tiny
window in which a thread switch could take place. Obviously the
wisdom of what he was doing was suspect, but the OP was right in that
a manual GIL release would allow a thread switch and could have helped
avoid starvation in that case.
Carl Banks
Oct 21 '08 #6
On Oct 21, 10:22*am, Carl Banks <pavlovevide...@gmail.comwrote:
On Oct 21, 5:09*am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Tue, 21 Oct 2008 04:58:00 -0200, Piotr Sobolewski *
<piotr_sobolew...@o2.plescribió:
But what about my main question? Is it possible to release GIL without
sleeping? I know that in this example situation I can achieve my goals
without that - I can just move sleep outside of locked block. But I
just want to know it for future - can I just do something like
thread.gil_release()?
No, you can't release the GIL *and* continue executing Python code.

I don't think that's what the OP was asking about; I think he merely
wanted to force a GIL release to give the other thread a chance to
run.

Reason being: in his code the lock was re-acquired right after it was
released. *This meant that the same thread that released the lock
almost always acquired it right back, since there was only a tiny
window in which a thread switch could take place. *Obviously the
wisdom of what he was doing was suspect, but the OP was right in that
a manual GIL release would allow a thread switch and could have helped
avoid starvation in that case.
If he released the GIL then he would still have to ensure that other
thread got a chance to claim it before the releaser tried to reclaim
it, which suggests the sleep that he was trying to avoid (unless the
release was really "yield the GIL to another thread if one is waiting
for it").
Oct 21 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Anthony Baxter | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.4 (release candidate 1). ...
0
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.5 (release candidate 1). Python 2.3.5 is a bug-fix release. See the release...
0
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.4.1 (release candidate 2). Python 2.4.1 is a bug-fix release. See the release...
0
by: Droopy Toon | last post by:
Hi, I think it is a FAQ but I found no answer to my problem. I created a C# program that called some C++ legacy code. I created 2 projects : 1) a DLL (IPRCommWrapper.dll) including an...
2
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.4.3 (release candidate 1). Python 2.4.3 is a bug-fix release. See the release...
0
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.4.4 (release candidate 1). Python 2.4.4 is a bug-fix release. While Python 2.5 is...
1
by: kurt sune | last post by:
I am having trouble publishing a website for RELEASE. 1. web.config: <compilation defaultLanguage="vb" debug="false"> 2. in Configuration manager I set the configuration to Release for the...
16
by: =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.5.2 (release candidate 1). This is the second bugfix release of Python 2.5. Python...
2
by: Piotr Sobolewski | last post by:
Hello, I have such program: import time import thread def f():     global lock     while True:         lock.acquire()         print thread.get_ident()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.