How to run multi python function witout wait for result...

143 views
Skip to first unread message

ali reza

unread,
Nov 23, 2020, 10:17:12 AM11/23/20
to gevent: coroutine-based Python network library
Greetings!!

I have a main function and i want to run multi python function without wait to finish (response is not important).
as test case, I write two simple func to test Concurrency/Parallelism/... . but didn't work.

def aaaa ():
    fw = None
    try:
        for a in range(1000):
            fw = open("0a.txt", mode="a+")
            #print ("a:" + str(a))
            fw.writelines("a:" + str(a) + "\n")
            sleep(0.5)
            fw.close()
    except KeyboardInterrupt:
        fw.close()

def bbbb ():
    fw = None
    try:
        for b in range(1000):
            fw = open("0b.txt", mode="a+")
            #print ("b:" + str(b))
            sleep(0.5)
            fw.writelines("b:" + str(b) + "\n")
            fw.close()
    except KeyboardInterrupt:
        fw.close()

I want both funcs to work at same time....
plz help me
Tnxxxxx

Jason Madden

unread,
Nov 23, 2020, 10:57:44 AM11/23/20
to gev...@googlegroups.com
You don't show us any of what you're doing to import APIs or launch greenlets, so I will have to make a bunch of guesses. (In the future, please try to provide complete examples. See http://sscce.org)

If you want to use greenlets and gevent to execute functions in parallel, the functions that you want to execute must cooperate with gevent. Be sure you've read and understood http://www.gevent.org/intro.html

Typically, that means you need to use gevent's modules, not the standard library modules, to perform things like sleeping and network IO.

This can be done by putting `from gevent import monkey; monkey.patch_all()` at the start of your script. (That's the way I usually recommend.) You may also choose to import specific gevent APIs, for example, `from gevent.time import sleep`.

You then need to start your functions in greenlets, and give them an opportunity to run.

Putting that together, your code should look something like this:

from gevent import monkey; monkey.patch_all() # MUST BE FIRST
import gevent
from time import sleep

def aaaa():
<your code here>
def bbbb():
<your code here>


greenlets = [gevent.spawn(aaaa), gevent.spawn(bbbb)]
gevent.joinall(greenlets) # Let them run; could do other gevent things here


It's important to note that regular files that you get from `open` generally DO NOT cooperate with gevent. That means you cannot read from or write to normal files in parallel (reading/writing a file blocks the gevent event loop). Usually reading/writing a regular file is so fast that this doesn't matter much. However, in the chance that profiling shows that reading from normal files is holding up your program (for example, because the files are actually located on a slow network file system) you'll need to use operating-system threads to achieve further concurrency in Python. You can do this in a bunch of ways, including using gevent's `FileObjectThread` (http://www.gevent.org/api/gevent.fileobject.html) as a wrapper (e.g., `with FileObjectThread(open("a.txt")) as fw:`).
> --
> You received this message because you are subscribed to the Google Groups "gevent: coroutine-based Python network library" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to gevent+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/gevent/08a70bad-269b-4e0e-8f98-d5f90e6dce5do%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages