Why can't I spawn() here? (error)

I have a GenStage and all I’m trying to do is spawn a process inside handle_events. Ultimately I want handle_events to return [] and then via message passing of the spawn() handle sending the events myself when they complete

def handle_events(users, _from, state) do
  endpoints = Config.get()[:endpoints]

  for user <- users do
    spawn(fn (user) -> do_user_bidding(user, endpoints) end)

  {:noreply, [], state}

The error I get is

handle_events/3> with arity 1 called with no arguments

Hey 9mm. For the future, please provide a full error message and stack trace when asking questions, it makes a big difference.

After some looking through I identified your issue:

spawn(fn (user) -> do_user_bidding(user, endpoints) end)

should be

spawn(fn -> do_user_bidding(user, endpoints) end)

The function you pass to spawn needs to be arity 0.

1 Like

if I do that it says

19:50:14.411 [error] Task #PID<0.365.0> started from #PID<0.318.0> terminating
** (BadArityError) #Function<4.86334033/1 in MyApp.EndpointAggregator.do_user_bidding/2> with arity 1 called with no arguments
    (elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
    (elixir) lib/task/supervised.ex:38: Task.Supervised.reply/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Function: &:erlang.apply/2
    Args: [#Function<4.86334033/1 in MyApp.EndpointAggregator.do_user_bidding/2>, []]

Is the code you’re showing what you actually have? Your stack trace talks about task pids but you don’t show anything using tasks.

1 Like

ugh crap… so that first problem you had me fix was the actual problem. I thought that was the ‘fixed’ way but it turns out I broke it. You helped me fix it, and now the broken code is in the function being called (I use tasks inside that do_user_bidding function)

Thank you!!

Real fast, do you know how to write this in Elixir? I’m confused on the #{} part

buoy:get(Url, #{timeout => 500}).

This is what i had so far…

:buoy.get(url, ...)

It works without the 2nd argument but then freezes

I keep getting 20:07:12.836 [error] Bad value on output port ‘tcp_inet’

and then my whole terminal freezes

Hmm… i have tried every combination I can think of. I have no idea what #{} is and i googling isnt all helpful for symbols

It’s the map syntax for erlang. :buoy.get(url, %{timeout: 500}).

yeah so weird, it just freezes. without it it works fine. I guess ill figure it out later