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

Home Posts Topics Members FAQ

I want to release the GIL

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)

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.

When I run it, I notice that only one thread works and the other one
never
has a chance to run. I guess it is because the thread don't have a
chance
to release the GIL - after it releases the lock, it almost immediately
(in
the very next bytecode command) reacquires it. I know I can
put "time.sleep(0.0 1)" command after between release and reacquire,
but it
doesn't seem elegant - why should I make my program sleep instead of
work?

Is there any simple way to release the GIL? Like:
lock.release()
thread.release_ gil()
?

Thanks in advance!
Oct 21 '08 #1
2 1637
On Mon, Oct 20, 2008 at 10:12 PM, Piotr Sobolewski
<pi************ **@o2.plwrote:
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)

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.

When I run it, I notice that only one thread works and the other one
never
has a chance to run. I guess it is because the thread don't have a
chance
to release the GIL - after it releases the lock, it almost immediately
(in
the very next bytecode command) reacquires it. I know I can
put "time.sleep(0.0 1)" command after between release and reacquire,
but it
doesn't seem elegant - why should I make my program sleep instead of
work?
You let *one thread* sleep so that the *other thread* can wake, grab
the lock, and *work*.

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
Is there any simple way to release the GIL? Like:
lock.release()
thread.release_ gil()
?

Thanks in advance!
--
http://mail.python.org/mailman/listinfo/python-list
Oct 21 '08 #2
On Oct 21, 1:12*am, Piotr Sobolewski <piotr_sobolew. ..@o2.plwrote:
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)
Let me state first of all that you are going about it all wrong.
Simple locks don't handle the above siutation very well because,
frankly, there's no benefit to using threads in this way at all.
Since neither thread can run at the same time as the other, you might
as well have just used ordinary looping.

Normally, when using threads, you only want to use locking for brief
periods when accessing data shared between threads, like so:

def f():
do_some_stuff
lock.acquire()
access_shared_d ata
lock.release()
do_some_more_st uff

Starvation is much less likely to occur in this situation. (The
interpreter periodically releases the GIL, so if there's stuff between
a release and the next acquire, it'll have a window in which to
release the GIL--unlike in your example where the window was very
tiny.)
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.

When I run it, I notice that only one thread works and the other one
never
has a chance to run. I guess it is because the thread don't have a
chance
to release the GIL - after it releases the lock, it almost immediately
(in
the very next bytecode command) reacquires it.
Not really the GIL's fault. Or rather, fiddling with the GIL is the
wrong way to get what you want here.

What you want is some kind of thread balancing, to make sure all
threads have a chance to run. Let me stress that, in this case, you
only need balancing because you're using threads incorrectly. But
I'll mention one way to do it anyway.

The easiest way I can think of is to use a counter that is protected
by a threading.Condi tion. One thread only runs when the counter is
even, the other only when it's odd. It might be implemented like
this:
counter = 0
c = threading.Condi tion()

def f():
global counter
c.acquire()
while counter % 1 == 0: # == 1 for the other thread
c.wait()
do_something()
counter += 1
c.notify()
c.release()
The reason threading.Condi tion is required and not a simple lock is
that simply acquiring the lock is not enough; the counter must be in
the right state as well.
Carl Banks
Oct 21 '08 #3

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...
6
2774
by: Delaney, Timothy (Tim) | last post by:
Piotr Sobolewski wrote: 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).
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...
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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...
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
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.

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.