# How do I run a function in a process and afterwards terminate the process?

**URL:** https://discourse.julialang.org/t/how-do-i-run-a-function-in-a-process-and-afterwards-terminate-the-process/66056
**Category:** New to Julia
**Tags:** question, distributed
**Created:** [August 9, 2021, 4:55am UTC](https://discourse.julialang.org/t/how-do-i-run-a-function-in-a-process-and-afterwards-terminate-the-process/66056 "2021-08-09T04:55:59Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![pitsianis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitsianis/32/26588_2.png) [@pitsianis](https://discourse.julialang.org/u/pitsianis)
#### Post date: [August 9, 2021, 4:55am UTC](https://discourse.julialang.org/t/how-do-i-run-a-function-in-a-process-and-afterwards-terminate-the-process/66056/1 "2021-08-09T04:55:59Z")

</div>

Hello!  
Suppose I have the module `Amodule` containing a function:

```julia
module Amodule

function twice(x)
  2 .* x
end

end

```

I need to run `Amodule.twice` on a worker process that I terminate afterward from another module:

```julia
module Bmodule

using Distributed

@everywhere include("Amodule.jl")
@everywhere using Main.Amodule

function test(x)
 
  pid = addprocs(1)[1]

  F = @spawnat pid Main.Amodule.twice(x)
  M = fetch( F )
  rmprocs( workers()... )

  all( M .== 2 .* x)
end

end

```

But

```julia
ERROR: LoadError: On worker 2:
UndefVarError: Bmodule not defined

```

How do we correct the `Bmodule` so that the `test` function can create a worker process, run `Amodule.twice(x)`, and then kill it?

Even if I replace the remote call with a trivial

```julia
F = @spawnat pid 1+1

```

the worker fails when it tries to find `Bmodule`.

I guess I do not understand the semantics of `@spawnat`. The child process does not inherit the context of the parent? How can I inform the child process about the `Bmodule` within the `Bmodule`?

Thanks!
