473,399 Members | 2,478 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,399 software developers and data experts.

context managers and co-routines

I'm happily using context managers and co-routines, and would like to
use both at the same time, e.g.

with foo():
...
x = yield y
...

In this code multiple copies of this code can be executing at the
"same" time, interleaved by the yield statement. This doesn't work
well, since the context manager is dealing with global state
(specifically, re-routing stdout).

The problem is that all of my state (local variables, etc.) is nicely
saved over the yield, but the context is not. So I end up having to
write the code like this:

with foo():
...
x = yield y
with foo():
...

which is not so pretty. What I'd like is some way to transparently save
and restore context over a yield, but I don't see an easy way to do
this.

Any suggestions?

Thanks,
Bob Sidebotham

Jan 12 '07 #1
3 1239
Jean-Paul Calderone wrote:
On 12 Jan 2007 06:17:01 -0800, bo************@gmail.com wrote:
I'm happily using context managers and co-routines, and would like to
use both at the same time, e.g.

Python has generators, not co-routines.
They may be called generators, but I'm using them as co-routines.
Wrap the generator in a function which co-operates with your context
managers to clean-up or re-instate whatever they are interacting with
whenever execution leaves or re-enters the generator.
I don't think wrapping the generator will do it. I think I would have
to wrap the actual yield call. Maybe that wouldn't be so bad.

There's not even any guarantee, by the way, that the yield even
returns: an exception outside the generator will not trigger the
exception in the context manager. In general, there is no hook (that I
can see) that helps with this.

I'm still thinking there is a better way to do it, and would appreciate
any ideas.

Bob

Jan 12 '07 #2
I'm still thinking there is a better way to do it, and would appreciate
any ideas.
Everything can be implemented with goto and arrays.

http://entrian.com/goto/

:-P


Jan 12 '07 #3
Laszlo Nagy wrote:
>I'm still thinking there is a better way to do it, and would
appreciate any ideas.

Everything can be implemented with goto and arrays.
But is that really better?

Regards,
Björn

--
BOFH excuse #174:

Backbone adjustment

Jan 12 '07 #4

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

Similar topics

1
by: SPG | last post by:
Hi, I am relatively new to TOMCAT, and am trying to deploy a web app using it. I have a domain name that I want to use to access my web app directly. By default, I deploy my WAR, and it deploys...
1
by: Paul Aspinall | last post by:
Hi I occassionally get the error: The requested lookup key was not found in any active activation context when I try to use Process.Start
2
by: jamil | last post by:
I have a tab control on a form with several tab pages. I have also added a context menu to the tab control that contains a couple of menu items-- Add and Delete. What I would like to do is...
1
by: balukrish | last post by:
How to access the file managers and storage managers with java codings?
5
by: Khafancoder | last post by:
Hi guys, i am building a FileSharing website, i wanna allow users to be able using download managers such as DAP to download files from webserver but don't allow them to retrive files url......
1
by: skip | last post by:
I am working on a file locking class which I'd like to work with Python 2.5's context managers. The acquire method takes an optional timeout argument: class FileLock: ... def acquire(self,...
4
by: Karl | last post by:
Hi all, I want to write an application that is launched from the context menu in Windows Explorer/Computer. That is to say, when I am browsing around my hard drive and get to any location I...
3
by: Floris Bruynooghe | last post by:
Hi I was wondering when it was worthwil to use context managers for file. Consider this example: def foo(): t = False for line in file('/tmp/foo'): if line.startswith('bar'): t = True
2
by: lairpr74 | last post by:
I am trying to update the managers name in active directory with an application I got that updates active directory. I been strugling with this for a few weeks now and couldn't find the best way...
4
by: Daniel | last post by:
Hello, I need to access the code inside of a context manager, i.e. the call to with myManager(v=5) as x: a=b c=sin(x) should cause the following output (minus the first line, if that's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.