简体   繁体   中英

I don't want my program to wait for a function to finish before moving on to the next task in Python

My code looks something like this:

def func1():
  page = requests.get(url)
  'click on something'

def func2():
  data = input()
  for x in range(10):
    func1()
    data = input()

Basically I want the code to move on and allow the user to enter new data and start another call to func1() immediately. However, currently Python waits for the call to func1() to finish before moving on.

To achieve what you want, you need to either use multithreading or multiprocessing , a simple example using multithreading :

from threading import Thread

def func1():
  page = requests.get(url)
  'click on something'

def func2():
  data = input()
  for x in range(10):
    t = Thread(target=func1)
    t.daemon = True
    t.start()
    data = input()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM