Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to share a group of functions with multiple main processes?

by ThelmaJay (Novice)
on Nov 19, 2013 at 16:18 UTC ( #1063352=perlquestion: print w/ replies, xml ) Need Help??
ThelmaJay has asked for the wisdom of the Perl Monks concerning the following question:

Dear Masters,

how can I share the same group of functions with different processes without having make copies of the same code.

Imagine I have a file printVar.pl with the content:

sub setVarValue{ ($inputVal) = @_; $v = $inputVal; return $v } 1;

and two processes:

Main one.pl

require 'C:\Users\Administrator\workspace\GPSTracker\lib\printVar.pl'; while(1){ $newVal = setVarValue(1); print "MAIN ONE - NEW VAL = $newVal\n"; } 1;

Main Two.pl

require 'C:\Users\Administrator\workspace\GPSTracker\lib\printVar.pl'; while(1){ $newVal = setVarValue(2); print "MAIN TWO - NEW VAL = $newVal\n"; } 1;

Now I would like to run both processes at the same time.But when I run one first the other blocks.

Is there a way to share resources between processes without interference

.

Comment on How to share a group of functions with multiple main processes?
Select or Download Code
Re: How to share a group of functions with multiple main processes?
by hippo (Curate) on Nov 19, 2013 at 17:36 UTC

    Some hints:

    1. Use modules.
    2. Nothing in your code blocks (that I can see), but you have infinite loops printing vast swathes of data, so it might seem that way.
    3. Really, use modules.
Re: How to share a group of functions with multiple main processes?
by Eily (Chaplain) on Nov 19, 2013 at 17:45 UTC

    How do you run your two perl scripts? If it was indeed impossible for multiple scripts to use (well, require in your case) the same module at the same time, that issue would have been raised long ago.

    I recommend you read Perl Modules. To make it short, if PrintVar.pm (something that is not a standalone Perl script but should be included, ie a module should have the .pm extension) is in the same folder as your script, to include it you just have to write: use PrintVar. And if it is in Something, use Something::Printvar;. This would allow you to move all your files at once without having to change every include path. You should still read Perl Modules if you want to make "good" Perl modules, to learn about packages (namespaces) and exporting.

Re: How to share a group of functions with multiple main processes?
by Laurent_R (Parson) on Nov 19, 2013 at 17:52 UTC
    You could use a module from 200 different processes and it will not block anything, actually not even slow down your processes in any significant fashion. Each process will actually have its own compiled version of the module, so that the processes remain independent from each other.
Re: How to share a group of functions with multiple main processes?
by RichardK (Priest) on Nov 19, 2013 at 18:59 UTC

    I'm not clear exactly what you are trying to share. Is it code or dynamic data? If it's just code then a module will do.

    If you need shared memory then that's more complex, you'll need to use some form of IPC.

Re: How to share a group of functions with multiple main processes?
by ThelmaJay (Novice) on Nov 23, 2013 at 16:26 UTC
    Thank you all for your kind answers. I think that is a module that I want.

    For example, I have to determine if a certain point (x,y) is inside a certain polygon. For that I have several functions. Then I have 3 different processes that run simultaneously. But each of them have at a certain time in the code to determine if a point is inside or out of an area.

    I just need to give as an input to those functions the point and the area. So I guess module is what I want.

    Just a question if I call in each process use moduleX.

    That means that I'm going to use 3 times the memory needed to load moduleX?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1063352]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-11-21 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (114 votes), past polls