Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Is it possible to perform concurrent action like loop?

by anbutechie (Sexton)
on Mar 20, 2009 at 04:45 UTC ( [id://751936]=perlquestion: print w/replies, xml ) Need Help??

anbutechie has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
Lets take,
foreach (0..5) { action like printing some string, or adding two numbers, etc... }
Is it possible to do the action cuncurrently?
Regards,
Anbarsu

Replies are listed 'Best First'.
Re: Is it possible to perform concurrent action like loop?
by ig (Vicar) on Mar 20, 2009 at 04:56 UTC

    It is possible to perform actions concurrently, either by creating threads or processes which can run concurrently.

    You can learn about these options by reading perlipc, perlfork, perlthrtut and Threads, Multi-Processing, and Inter-Process Communication. It is a broad topic with many issues, so you should expect to invest considerable time and effort in learning enough to be successful and proficient.

    It would be unusual to use concurrent threads or processes for operations as simple as printing some strings or adding some numbers but doing so may be valuable as a learning exercise.

Re: Is it possible to perform concurrent action like loop?
by BrowserUk (Patriarch) on Mar 20, 2009 at 08:30 UTC
      async { action like printing some string, or adding two numbers, etc +... }->detach;

      ?


      "Half of all adults in the United States say they have registered as an organ donor, although only some have purchased a motorcycle to show that they're really serious about it."
Re: Is it possible to perform concurrent action like loop?
by cdarke (Prior) on Mar 20, 2009 at 08:16 UTC
    Maybe, maybe not - it depends on how related the actions are and if you consider "possible" to be "desirable". For example, if printing was done to the same place then the output would be interleaved and would appear corrupted. This can happen easily when the same buffer is used by all threads. So yes, it is possible, but you might not get what you want.

    Adding two numbers is also not a simple as it first appears. First, of course, each thread must retain its own result. If you then require a summation then a "sweeper" thread must access each worker thread's result to produce the sum. Not everything can be made parallel, see Amdahl's law.
Re: Is it possible to perform concurrent action like loop?
by zentara (Archbishop) on Mar 20, 2009 at 10:23 UTC
    Well, no one has mentioned an Event-loop system yet, like POE, or Tk or Gtk2. Also see Roll your own Event-loop.

    With an event loop ( gui toolkits are eventloop based), you can setup timers that touch callbacks every defined time interval. It effectively allows you to run things simultaneously.


    I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
      It effectively allows you to run things simultaneously.

      Um, no. Not even close.

      Event loops run everything serially, in the same single thread. They will not benefit from multi-cores or multi-processors.

      And if those things are cpu-bound, they will (collectively) run more slowly than if you just run them one after the other in the normal way.

      Events loops are only useful for code that spends most of it's doing nothing but wait. Eg. communications servers and guis.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Well, his original code example was a simple count. He could have multiple timers running in an event-loop system, counting multiple things, and unless he pushed system load limits, it would run fine.

        Saying the loop runs things sequentially is true, but misleading... because in a single processor computer, all processes get run sequentially anyways.....the execution pointer can only be in one place at a time. So event-looping is just extending what the kernel does, on a per-process basis.

        But yes, if your loops are resource hogs, it may be neccessary to fork or thread.

        It's like using IO::Select....do you avoid "select" because it's usage usually loops over it's select array? No, you use it, but keep in mind it's limitations.


        I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
Re: Is it possible to perform concurrent action like loop?
by Bloodnok (Vicar) on Mar 20, 2009 at 11:26 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-03-28 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found