473,503 Members | 5,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Embedding: Is it possible to limit number of virtual instructionsexecuted?

Hi experts-

I have a potential use case of embedding Python where it must co-
operate with the host c/c++ application in a single thread. That is,
the c code and python interpreter need to cooperate and time share, or
yield to each other.

The main loop would need to do something like:

check for events
for events with c handlers:
process in c
for events with python handlers:
add to queue
if have time:
python_continue_execution(number of vm instructions)

The goal is to be able to set some limit on the amount of time in the
interpreter in each loop, and not rely on the python side code. (I
realise this will not be absolute, especially if the python code calls
c extensions, but it will be more straight forward for the users.)

Does anyone have any suggestions for how this could be achieved?

Thanks in advance,

Julian
Nov 1 '08 #1
7 1038
Does anyone have any suggestions for how this could be achieved?

You'll have to adjust Python/ceval.c. Look for _Py_Ticker, which
provides some fairness for Python threads (releasing the GIL after
_Py_CheckInterval instructions). If you decrement another global
variable there, you can determine that the limit has been reached,
and raise an exception (say).

Regards,
Martin
Nov 1 '08 #2
ju************@gmail.com wrote:
Hi experts-

I have a potential use case of embedding Python where it must co-
operate with the host c/c++ application in a single thread. That is,
the c code and python interpreter need to cooperate and time share, or
yield to each other.

The main loop would need to do something like:

check for events
for events with c handlers:
process in c
for events with python handlers:
add to queue
if have time:
python_continue_execution(number of vm instructions)

The goal is to be able to set some limit on the amount of time in the
interpreter in each loop, and not rely on the python side code. (I
realise this will not be absolute, especially if the python code calls
c extensions, but it will be more straight forward for the users.)

Does anyone have any suggestions for how this could be achieved?
Does this help at all?
>>help(sys.setcheckinterval)
Help on built-in function setcheckinterval in module sys:

setcheckinterval(...)
setcheckinterval(n)

Tell the Python interpreter to check for asynchronous events every
n instructions. This also affects how often thread switches occur.

I *don't* know any more that this.

tjr

Nov 1 '08 #3
ju************@gmail.com writes:
The goal is to be able to set some limit on the amount of time in the
interpreter in each loop, and not rely on the python side code. ...
Does anyone have any suggestions for how this could be achieved?
Not really. Limiting the number of virtual instructions (byte codes)
is of no help, since each one can consume unlimited runtime. I think
for example that bigint exponentiation is one bytecode, and 9**999999
probably takes several seconds to compute.
Nov 1 '08 #4
Does anyone have any suggestions for how this could be achieved?

You'll have to adjust Python/ceval.c. Look for _Py_Ticker, which
provides some fairness for Python threads (releasing the GIL after
_Py_CheckInterval instructions). If you decrement another global
variable there, you can determine that the limit has been reached,
and raise an exception (say).

Regards,
Martin
Nov 1 '08 #5
ju************@gmail.com wrote:
Hi experts-

I have a potential use case of embedding Python where it must co-
operate with the host c/c++ application in a single thread. That is,
the c code and python interpreter need to cooperate and time share, or
yield to each other.

The main loop would need to do something like:

check for events
for events with c handlers:
process in c
for events with python handlers:
add to queue
if have time:
python_continue_execution(number of vm instructions)

The goal is to be able to set some limit on the amount of time in the
interpreter in each loop, and not rely on the python side code. (I
realise this will not be absolute, especially if the python code calls
c extensions, but it will be more straight forward for the users.)

Does anyone have any suggestions for how this could be achieved?
Does this help at all?
>>help(sys.setcheckinterval)
Help on built-in function setcheckinterval in module sys:

setcheckinterval(...)
setcheckinterval(n)

Tell the Python interpreter to check for asynchronous events every
n instructions. This also affects how often thread switches occur.

I *don't* know any more that this.

tjr

Nov 1 '08 #6
ju************@gmail.com writes:
The goal is to be able to set some limit on the amount of time in the
interpreter in each loop, and not rely on the python side code. ...
Does anyone have any suggestions for how this could be achieved?
Not really. Limiting the number of virtual instructions (byte codes)
is of no help, since each one can consume unlimited runtime. I think
for example that bigint exponentiation is one bytecode, and 9**999999
probably takes several seconds to compute.
Nov 1 '08 #7
Thanks to those who replied; it seems that it is possible to do,
but would require some changes to the python core, and may not be
as fine-grained as I'd presumed. I think I'll consider running
python in a seperate thread and create a couple of message queues
to/from that thread and the main thread.

Julian

On Nov 1, 12:46*am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
juliangrend...@gmail.com writes:
The goal is to be able to set some limit on the amount of time in the
interpreter in each loop, and not rely on the python side code. ...
Does anyone have any suggestions for how this could be achieved?

Not really. *Limiting the number of virtual instructions (byte codes)
is of no help, since each one can consume unlimited runtime. *I think
for example that bigint exponentiation is one bytecode, and 9**999999
probably takes several seconds to compute.
Nov 4 '08 #8

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

Similar topics

10
2101
by: Ken Ward | last post by:
Hi All, I'm a new boy to php and working on a web site with long sections of code that could be done as includes apart from one or two things changing. I've tested the following and it works,...
1
3160
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
13
3983
by: Rolf Magnus | last post by:
Hi, I would like to embed a python interpreter within a program, but since that program would be able to automatically download scripts from the internet, I'd like to run those in a restricted...
4
1299
by: WindAndWaves | last post by:
Hi Gurus Here is another one - I am just soaking up all these new learnings! I have the following piece of html <IMG SRC="test.gif" WIDTH="320" HEIGHT="256" ALT="movie placeholder"> <BR>...
4
1597
by: Bruce | last post by:
I thought that I had read somewhere before about a limit to the number of DLL's that could be loaded by a 32 bit application when it is run on Windows 98/ME operating systems. But I can't seem...
0
2132
by: Kenneth McDonald | last post by:
Sorry for crossposting to several lists, but from what I can tell, what I want to do may involve several different areas of expertise. (None of which I have :-( ) I'd like to use Gecko as the UI...
4
5785
by: Daniel | last post by:
is there some per-process-limit on memory in .net processes? is there any way to increase it? i keep getting System.OutOfMemoryException when my box has 8 gigs of unused memory.
5
12616
by: Max2006 | last post by:
Hi, What is the limit for memory that a .NET process or AppDomain can use? Thank you, Max
0
2090
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
0
7188
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
7258
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
7313
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...
1
6970
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
7441
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...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
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...
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
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.