473,394 Members | 1,951 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,394 software developers and data experts.

Ideas for yielding and exception raising

I was wondering if it was possible, now or with a patch, to do either of the
following:

1) Cause another thread's most recent function, or a given function in a
given thread, to yield its generator immediately, such that the generator
can be used to pick back up where it left off. That is, the function itself
wouldn't need an actually yield keyword. Could be used to allow a function
to be controlled in how much time it has per second or somewhat?

2) Cause an exception to be raised in all threads that have a reference to a
given object, as if the object itself is raising the exception. This would
allow functions to return Monitor Objects that would raise exceptions if
something bad happened. For example, maybe you want the function to run
continually until someone else requests access to some syncronized data,
and the Monitor would raise the exception at that time.
Jul 18 '05 #1
3 1282
Calvin Spealman wrote:
I was wondering if it was possible, now or with a patch, to do either of the
following:

1) Cause another thread's most recent function, or a given function in a
given thread, to yield its generator immediately, such that the generator
can be used to pick back up where it left off. That is, the function itself
wouldn't need an actually yield keyword. Could be used to allow a function
to be controlled in how much time it has per second or somewhat?

2) Cause an exception to be raised in all threads that have a reference to a
given object, as if the object itself is raising the exception. This would
allow functions to return Monitor Objects that would raise exceptions if
something bad happened. For example, maybe you want the function to run
continually until someone else requests access to some syncronized data,
and the Monitor would raise the exception at that time.


Wouldn't it be better for both of these situations to do it
explicitly and actually write the code that way? What is
the advantage in putting this in the core and having it
happen magically and behind the scenes, as it appears you
want to happen?

-Peter
Jul 18 '05 #2
Peter Hansen wrote:
Calvin Spealman wrote:
I was wondering if it was possible, now or with a patch, to do either of
the following:

1) Cause another thread's most recent function, or a given function in a
given thread, to yield its generator immediately, such that the generator
can be used to pick back up where it left off. That is, the function
itself wouldn't need an actually yield keyword. Could be used to allow a
function to be controlled in how much time it has per second or somewhat?

2) Cause an exception to be raised in all threads that have a reference
to a given object, as if the object itself is raising the exception. This
would allow functions to return Monitor Objects that would raise
exceptions if something bad happened. For example, maybe you want the
function to run continually until someone else requests access to some
syncronized data, and the Monitor would raise the exception at that time.


Wouldn't it be better for both of these situations to do it
explicitly and actually write the code that way? What is
the advantage in putting this in the core and having it
happen magically and behind the scenes, as it appears you
want to happen?

-Peter


How would/could I do this explicitly?
--

Jul 18 '05 #3
(Reordered questions to make the answers appear next to them.)

Calvin Spealman wrote:
Peter Hansen wrote:
Wouldn't it be better for both of these situations to do it
explicitly and actually write the code that way? What is
the advantage in putting this in the core and having it
happen magically and behind the scenes, as it appears you
want to happen?


How would/could I do this explicitly?

Calvin Spealman wrote:
1) Cause another thread's most recent function, or a given function in a
given thread, to yield its generator immediately, such that the generator
can be used to pick back up where it left off. That is, the function
itself wouldn't need an actually yield keyword. Could be used to allow a
function to be controlled in how much time it has per second or somewhat?
To a large extent that is unanswerable without actually writing
your code. If you know how to use generators, for example, then
the answer is "use generators"... All I meant here is that you
should actually *use* the yield keyword, writing the functions
that you want to control so that they give up the processor to the
calling routine at various intervals in a way that allows you to
control them as you wish. Of course, this just becomes a form of
cooperative multitasking, so you could also write it using threads
and voluntary checking of a "terminate" flag. With Python a
thread that does not block in an extension module cannot consume
all the CPU time unless it has been written deliberately to be
disruptive. The interpreter will only execute sys.checkinterval()
bytecode instructions at a time before switching to another thread.
2) Cause an exception to be raised in all threads that have a reference
to a given object, as if the object itself is raising the exception. This
would allow functions to return Monitor Objects that would raise
exceptions if something bad happened. For example, maybe you want the
function to run continually until someone else requests access to some
syncronized data, and the Monitor would raise the exception at that time.


This one I'm not even sure I understand, as you haven't really
described what you are trying to do. ("something bad happening" is
not an adequate description. :-) ) It sounds like you want
the benefits of synchronization without any of the disadvantages.
If that's so, then you want to implement it yourself again in a
somewhat cooperative fashion, having the "function run continually"
but periodically check a flag that is set when another thread asks
for access. The first thread then blocks itself and releases
control of the shared resource, allowing the second one in.

Having written both the above attempts at answers, it seems to me
that in both cases you are trying to write some kind of threaded
code but without some of the disadvantages of threads. If you
are going to do threading, you should be very clear about what
kinds of synchronization you do or there are many ways it will
bite you in the ass. That's really what I mean about being
explicit. Use a Queue, write the code so that it's very clear
about where and when a generator yields control, or use whatever
other mechanisms are provided to do this already.

Anyway, changes to the core won't even be considered unless someone
can present some very compelling use cases for the behaviour.

And a patch.

-Peter
Jul 18 '05 #4

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

Similar topics

1
by: Rajesh.V | last post by:
I have made a custom control in asp.net using webcontrols, and am raising my event. I find that if no control is hooked into the event it goes for a toss. I tried to find length of the delegate...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
5
by: Bevo | last post by:
In my enterprise application I'd like my DataAccess layer to be responsible for handling all database error situations and to basically throw custom exceptions according to whether db connection is...
4
by: Prince Mathew | last post by:
Hi All, I have a requirement. I am throwing an exception from the Page_Load of my user control I want to catch this in my container page. Is this possible? The Page_Load of user control is...
0
by: Apu Nahasapeemapetilon | last post by:
Suggestions on cross-posting this issue would be appreciated. I've got a C# .NET DLL (CLR v1.1) that raises events into its container. When the container is a .NET application, raising the...
9
by: Jens Jensen | last post by:
Hello all, I need some design advice for my web service. I' have written a web service that exposes a function that takes some parameters and return an xml.
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
1
by: Timmy | last post by:
Hi, I has a question about exception in python. I know that an exception can be re-raised. Is there any simple way provided by python itself that I can know the current exception is just firstly...
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
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
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...
0
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...
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
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...

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.