TELunus
problem asynchronizing ecto calls
I seem to be having an issue making some code asynchronous. I’m running
stream = Task.Supervisor.async_stream_nolink(
Module.TaskSupervisor,
enum,
fn (item) -> my_func(item, 2) end,
[ordered: false, max_concurrency: 1]
)
results = Enum.to_list(stream)
which gives me 12:31:22.290 [error] Postgrex.Protocol (#PID<0.678.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.1768.0> exited while client #PID<0.1771.0> is still running with: shutdown
part of the code that’s run by my_func is an ecto call
IO.puts("before")
Repo.get(schema, id)
IO.puts("after")
and I’m seeing the "before" get printed right before the error message, but I’m not seeing the "after"
On the other hand if I change my code to run synchronously as
stream = Enum.map(enum, fn (item) -> my_func(item, 2) end)
results = Enum.to_list(stream)
then everything works fine. There’s no error message, and both print statements get run.
Any thoughts on what I could be doing wrong or how I can fix this?
Most Liked
alco
There’s another option now: update your deps and it should just work - Plataformatec on X: "We have just released DBConnection v2.0.4 (which powers Ecto) to use Elixir's v1.8 task callers feature. This means using the SQL Sandbox to test code that uses tasks for concurrency should work out of the box - and it should work concurrently too (wow, such concurrency)!" / X
alco
Just wanted to comment on this. The team working on Elixir does their best to keep Elixir versions backwards-compatible. The worst thing you should get after upgrading to a newer version is a bunch of new deprecation warnings.
I would actually encourage you to upgrade early because if something does get deprecated, you can stop doing that in your code and reduce the amount of work that you would otherwise have to do if you decided to upgrade months later. Dependencies also keep working as before as a rule, but since you may start getting new warnings, that can be something that prompts you to look into gradually upgrading your dependencies as well sooner rather than later. This is again going to be easier than skipping a few version of Elixir and getting a lot more issues with dependencies when you finally decide to upgrade.
TELunus
It looks like the project is already doing that. The test file starts with
defmodule Project.Module do
use Project.ModelCase
And in model_case.ex we’ve got
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Project.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Project.Repo, {:shared, self()})
end
:ok
end
So since :async isn’t included in the use line I believe that means we’re using shared mode, so any process should be able to connect to the DB connection.
benwilson512
I really don’t recommend this. If you’re testing or developing with a database type other than what you use in production you’re setting yourself up for great production surprises.
SZJX
There’s a special need in this project where the program needs to run on a local computer of somebody who doesn’t have any programming knowledge. I used Docker in the beginning but then decided that SQLite + an executable produced by distillery would be the ultimate one-click solution. Of course it wouldn’t make sense to test or develop with a different DB type.
I guess a more reasonable thing for me to do might be to just open a different branch for that, while keeping the main version to be run on the server on the main branch. Now I’m creating an extra :local environment for this purpose, that’s why the abovementioned happened.








