calling function while it is still running

This is the place for queries that don't fit in any of the other categories.

calling function while it is still running

Postby EccentricDyslexic » Sun Jul 27, 2014 7:56 pm

I have a python code that calls a function, this function takes 61 seconds to run end to end. what if the same function is called again while the first instance is still running? does another instance start up?

cheers

steve
EccentricDyslexic
 
Posts: 7
Joined: Fri Jul 25, 2014 7:01 pm

Re: calling function while it is still running

Postby metulburr » Sun Jul 27, 2014 8:20 pm

I have a python code that calls a function, this function takes 61 seconds to run end to end. what if the same function is called again while the first instance is still running? does another instance start up?

only if you start a new thread or process, otherwise your waiting for that initial one to end.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: calling function while it is still running

Postby EccentricDyslexic » Sun Jul 27, 2014 8:30 pm

sorry i dont understand.

the main sub of the program calls opengate() when it sees a known license plate. opengate operates a relay for a second, closes it and then waits for 60 seconds before exiting the function.

while this is happening, the main sub (i think) is still possibly sending out more opengate() requests(as it can still see the license plate waiting at the gate). are these additional opengate() requests actioned or ignored until opengate has exited its own routine?

I hope that is a little clearer,

many thanks

steve
EccentricDyslexic
 
Posts: 7
Joined: Fri Jul 25, 2014 7:01 pm

Re: calling function while it is still running

Postby EccentricDyslexic » Tue Jul 29, 2014 8:11 pm

By thread or process do you mean function call ie opengate() or another instance of the whole program itself?

cheers

steve
EccentricDyslexic
 
Posts: 7
Joined: Fri Jul 25, 2014 7:01 pm

Re: calling function while it is still running

Postby EccentricDyslexic » Wed Jul 30, 2014 8:05 pm

Hello?
EccentricDyslexic
 
Posts: 7
Joined: Fri Jul 25, 2014 7:01 pm

Re: calling function while it is still running

Postby micseydel » Wed Jul 30, 2014 10:45 pm

When you write Python code, it is run serially, not in parallel. If you have this code
Code: Select all
from time import sleep

def call():
    print "Beginning call()"
    sleep(61)
    print "Completed call()"

call()

call()

where the first call() takes 61 seconds, the the second one will not start until the first is finished.

The typical way to get them to run at the same time is threading. It takes extra effort to get concurrency. People new to programming or Python don't tend to get to it until they have learned quite a bit more.

You seem to be assuming that a subsequent call in your code will begin immediately after the first begins. So with the information you've provided so far (without any code), we can't tell if it's a misconception on your part or if you're using a specialized framework or library in which things actually are done in parallel, which if that's the case, we can't answer your question anyway without knowing how that framework/library does things. With regular threads it is a new "instance" as you said, and it seems likely that if you are using something specialized that the first function call wouldn't be killed by the second one beginning, but we can't answer with certainty.

I hope I've been clear. If not, please provide more information in any subsequent questions, and avoid assuming that mentioning "opengate()" out of context will help us to help you.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1369
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], W3C [Linkcheck] and 4 guests