473,785 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_iden t()
* * * * time.sleep(1)
* * * * lock.release()
lock=thread.all ocate_lock()
thread.start_ne w_thread(f,())
thread.start_ne w_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_iden t()
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 2774
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_rele ase()?
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_rele ase()?

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_rele ase()?
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.a r>
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_rele ase()?

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.a r>
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_rele ase()?
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
1990
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). Python 2.3.4 is a bug-fix release. See the release notes at the website (also available as Misc/NEWS in the source distribution) for details of the bugs squished in this release.
0
1289
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 notes at the website (also available as Misc/NEWS in the source distribution) for details of the bugs squished in this release. Assuming no major problems crop up, a final release of Python 2.3.5 will follow in about a week's time.
0
1314
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 notes at the website (also available as Misc/NEWS in the source distribution) for details of the bugs squished in this release. Assuming no major problems crop up, a final release of Python 2.4.1 will be out around the 29th of March - straight...
0
1650
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 unmanaged C++ wrapper for legacy code and a managed C++ wrapper to allow C# to call this unmanaged wrapper 2) a C# Win application
2
1533
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 notes at the website (also available as Misc/NEWS in the source distribution) for details of the more than 50 bugs squished in this release, including a number found by the Coverity Scan project.
0
1122
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 the latest version of Python, we're making this release for people who are still running Python 2.4. See the release notes at the website (also available as Misc/NEWS in the source distribution) for details of the more than 80 bugs squished...
1
2998
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 website 3. in Configuration manager I set the Active solution configuration to Release
16
1709
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.5 is now in bugfix-only mode; no new features are being added. According to the release notes, over 100 bugs and patches have been addressed since Python 2.5.1, many of them improving the stability of the interpreter, and improving its...
2
1637
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
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10100
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.