Repository navigation
Conversation
This provides a public API to replace the private `_running` flag specific to PollIOLoop. Also mirrors equivalent APIs provided by asyncio and Twisted.
|
|
||
| finally: | ||
| # reset the stopped flag so another start/stop pair can be issued | ||
| self._running = False |
There was a problem hiding this comment.
This was not necessary for this PR, but it seems more robust to add this (if loop.start gets crashed by an exception, the "running" flag should be reset to False).
|
This is different and much simpler than what @mrocklin was asking for, though. |
|
Yeah, I'm more comfortable with this than the more complicated proposal of #2070 (especially since the long-term plan is for IOLoop to disappear in favor of asyncio's event loop). But doesn't #2070 (comment) indicate that this method doesn't actually meet your needs? |
|
Checking if a particular loop is currently running definitely remains a
useful operation, even if checking if we are "in an event loop" is error
prone and not useful.
…On Sat, Jun 17, 2017 at 9:57 PM, Ben Darnell ***@***.***> wrote:
Yeah, I'm more comfortable with this than the more complicated proposal of
#2070 <#2070> (especially
since the long-term plan is for IOLoop to disappear in favor of asyncio's
event loop). But doesn't #2070 (comment)
<#2070 (comment)>
indicate that this method doesn't actually meet your needs?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2085 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AASszBHdSJ1FJF37uzro4KKDRfIly5uiks5sFIPwgaJpZM4N3PDy>
.
|
|
Can you elaborate on this use case? I'm still not seeing where this would be useful. (and I fear that in practice people would use it incorrectly to check for "in an event loop") |
|
Looking through the codebase we currently use the private
|
|
It could be that we should handle all of these cases in different ways though. I'm not sure. Regardless, having access to the |
This seems like a very peculiar pattern to me. The usual idiom for initializing a Tornado app is to construct an I don't have the big picture of how this API is used, but IMHO it would benefit from passing around "executors" instead of IOLoop. Then you could have an executor to schedule callbacks on an IOLoop running on the current thread, or another one to schedule them on an IOLoop that the executor (lazily) starts in a background thread.
I can see why you need to block here to avoid starting two threads, given the above behavior. Would you still need this if you no longer relied on
What if the signal comes after a thread has been started to run the IOLoop, but before the loop has started? The decision to start the IOLoop is not atomic with the setting of the Do you ever stop your IOLoop before the program exits? If not, you could swap your signal handler to one that uses the IOLoop just before starting it. |
|
@bdarnell do you have reservations about this PR? The additions are quite simple IMHO, and the same API already exists on the asyncio loop. |
|
Yes, I have reservations about this change. We had this method before, and I can't recall ever seeing a use of it that wasn't buggy (and it seemed to be an especially attractive nuisance to people who hadn't quite grasped the right idioms to use for event loops). Even the uses proposed in this thread look like they're full of race conditions to me. The fact that asyncio has this method softens my resistance a little bit, since in Tornado 5.0 you'll be able to access the asyncio loop's |
|
Well, I'm not sure, but about this suggestion:
What if a signal arrives between registration of the signal handler and starting the IOLoop? |
The signal handler that uses the IOLoop would just use |
@bdarnell the use case in Dask is a bit different because we need to support our own concurrency alongside Jupyter's concurrency for user interactions. Given how Jupyter works we actually need two event loops, otherwise our application hangs whenever a user executes a long-running cell. This definitely isn't the common case, and is certainly suboptimal, but I suspect that it's also unlikely to change in the near-to-moderate future given Jupyter's wide-spread use (fixing this within Jupyter and telling everyone to upgrade isn't a viable solution for us). Having a way to check if the
|
|
That advanced use case is based on a subtle set of assumptions: Either the caller is responsible for starting the IOLoop, or Dask is. If the caller is responsible, they might do something like If Dask is responsible, you take on that synchronization burden yourself, which is feasible as long as the caller is single-threaded. If the caller is multi-threaded, though, you can no longer coordinate among the different caller threads and multiple of them may try to start the IOLoop simultaneously. In short, I think it's important to be explicit about who will start an IOLoop. My suggestion is that the creator of the loop should always be responsible: if a loop is passed in, the caller is responsible for starting it. I'm not sure what the lifecycle of your |
|
In practice users interact from a single thread and aren't comfortable with the |
|
I don't disagree with your suggestions, and looking through the code here has given me ideas to reduce our dependence on the |
|
This seems very useful. Going through my code I use it only to detect when it's IOLoop is not running:
Only in one case (some magic code to handle async test setUp) the code using access to |
|
I'd like to second what @friedcell said about tests. It's important to check that a test didn't leave a loop running (for example in another thread). For that I haven't found any other solution than inspecting a |
|
I understand @friedcell's desire for better error reporting when you have both sync and async tests. (but note that we now have better "ioloop is already running" messaging than we used to, so as long as it's the same IOLoop you can just run it and let the IOLoop report the error. Are you asking about being able to do this for something other than AsyncHTTPTestCase.fetch on the same IOLoop used by gen_test?) I don't understand the "prevent reconnects when server is shutting down" - nothing happens after the IOLoop exits, so when would you ever observe I also don't quite get @pitrou's concern about an IOLoop left running. A running IOLoop must be running in a thread, so A) you can ask the thread if it's running regardless of whether IOLoop supports this, and B) you generally don't just want to ask the thread, you want to |
|
@bdarnell in test code I'll have helpers that have a sync and async version depending on type of test - to nicely error out when wrong type is used I'm currently inspecting The reconnect case is checking |
Agreed as long as the test harness started the thread itself (and therefore knows about it). But sometimes we pass a loop and it will be started by some of the code under test. Unless by peeking into |
|
Stepping back a moment, it seems like having some indication of if an IOLoop is started or not is useful to a variety of people in a variety of atypical situations for debugging, diagnostics, testing, and so on. They may not be using Tornado in an ideal way, but they are using it in a way that they find useful for their application. Turning around the discussion a bit, why not provide some indication of if an IOLoop is running? What harm does this cause? Is this just about stopping users from shooting themselves in the foot? If so then perhaps there are other ways to warn users about the proper use of this method, such as with loud warnings in documentation or docstrings. These methods might suitably indemnify Tornado maintainers from dealing with problems here while still allowing Tornado users control if they choose to take it. As someone who uses Tornado a fair amount these days I'm asking for bullets and I'm ready to accept the loss of a toe or two. |
It is attractive in a variety of situations. I'm not convinced it's useful.
Yes, it's about stopping users from shooting themselves in the foot. This method is inherently subject to race conditions. Let's look at how dask is using it: This function is only safe to call from multiple threads if the IOLoop is known to be running in advance. (Otherwise, it could fail in a variety of ways, including two threads trying to start the loop at once in their own Maybe thread safety is not required for this usage. That's fine, but in that case why even bother with the possibility of a shared IOLoop on another thread? Starting an IOLoop in another thread so that If I were designing this API without regard for backwards compatibility, I'd make the |
|
So that code in @pytest.fixture
def loop():
with pristine_loop() as loop:
yield loop
# Stop the loop in case it's still running
try:
sync(loop, loop.stop)
except RuntimeError as e:
if not re.match("IOLoop is clos(ed|ing)", str(e)):
raiseThere we want to cleanly shut down an IOLoop, regardless of if it is running or not. Usually our tests don't stop the IOLoop, but some do. In the context of this pytest fixture we actually don't know which situation we're in and need some mechanism to figure out what to do to stop things cleanly. Can you recommend a better solution here? I think that this is the last usage of (though, to be clear, even if dask is able to remove all uses of the |
|
My first suggestion[1] is to run If you want to wait for some sort of cleanup before you proceed to the next test, you probably want to join the thread where the IOLoop was running, not just stop the loop itself. [1] Actually, my first suggestion is to unconditionally start the IOLoop thread when creating it so there's never ambiguity about whether it's running or not. But since you're committed to ambiguous IOLoop states, this is how I'd handle it. |
But we also want to call |
|
GC will eventually free up the file descriptors if you don't close the IOLoop, but in tests it's easy to accumulate enough unclosed file descriptors to exhaust your limits, so you're right that it's necessary to close the loop. Unfortunately, |
|
@bdarnell I still think it would be better to add the function and in docs show all the ways you can shoot yourself in the foot with it. Currently a naïve approach to this is to find the I think all of us will survive if it's not added, but there will be more devs that hit this issue and will only find instructions and reasons for it not existing in this issue and not the docs. |
Uh, yes, thank you. I had missed that.
You're right... But there doesn't seem to be a better way for us to do it -- not with that API anyway. We can't even busy-loop before close() since there's no official way to introspect the running state of the loop. |
Right - |
|
This would be handy for me, where I've been doing some cleanup in a new, temporary eventloop ( from tornado.ioloop import IOLoop
async def outer():
async def inner():
print('ok')
IOLoop(make_current=False).run_sync(inner)
IOLoop.current().run_sync(outer)In my case, if I could check if the eventloop is running, I could hand it off, otherwise call it synchronously: if IOLoop.current().is_running():
IOLoop.current().add_callback(f)
else:
IOLoop().run_sync(f)I'm not super concerned with the cleanest or most robust solution to this, since it is only testing machinery and doesn't come up in real application code. I've chosen to follow what you've mentioned and create a dedicated thread for this, so that there is only one thread in the temporary loop. It works fine that way. |
OK, that's a regression (I thought we had tests for this, but I guess not). I remember fixing something to make the synchronous tornado.httpclient.HTTPClient work inside an event loop (this would block the loop, of course, but it was sometimes useful for transitions in a WSGIContainer when you didn't want to have a completely separate HTTP client like Unfortunately, since this is coming from inside asyncio, there's not much we can do about it besides lobbying asyncio to be less prescriptive in this respect (maybe a context manager to temporarily do But those are very different! You can't tell when (or whether) |
|
I'm still not a fan of this method, but if you want it, you can get it on python 3 with |
This provides a public API to replace the private
_runningflag specific to PollIOLoop.Also mirrors equivalent APIs provided by asyncio and Twisted.