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

Skip to first unread message

ali reza

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

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
        for a in range(1000):
            fw = open("0a.txt", mode="a+")
            #print ("a:" + str(a))
            fw.writelines("a:" + str(a) + "\n")
    except KeyboardInterrupt:

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

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

Jason Madden

Nov 23, 2020, 10:57:44 AM11/23/20
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

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

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` ( 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
> To view this discussion on the web visit

Reply all
Reply to author
0 new messages