6 Replies - 216 Views - Last Post: 18 March 2019 - 12:26 PM Rate Topic: -----

#1 wujiz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-March 19

Async background tasks?

Posted 18 March 2019 - 08:51 AM

Hi

I'm trying to get async background tasks working with python, but it seems the concurrent.futures module isn't working as how I expected or what is documented.

Sample code
    def a():
        print('A')
        time.sleep(5)
        print('Done A')

    def b():
        print('B')
        time.sleep(1)
        print('Done B')

    def c():
        executor = ThreadPoolExecutor(2)
        a()
        b()
        print('Done C')

    c()

    # output
    A
    Done A
    B
    Done B
    Done C


It seems to not be asynchronous? Can someone please help? I'd like a() and b() to be in the background, so the first output should have been "Done C"

Is This A Good Question/Topic? 0
  • +

Replies To: Async background tasks?

#2 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1364
  • View blog
  • Posts: 4,187
  • Joined: 12-May 09

Re: Async background tasks?

Posted 18 March 2019 - 09:30 AM

You're not sending any work to the executor.

Your program basically says:

1. make an executor!
2. run a
3. run b
4. print "Done C"

Instantiating an executor doesn't automatically run things asynchronously or in parallel. You need to use submit.
Was This Post Helpful? 0
  • +
  • -

#3 wujiz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-March 19

Re: Async background tasks?

Posted 18 March 2019 - 09:32 AM

Whoops sorry I didn't see that typo. I do have submit() in my code, but it's printing out that output

executor.submit(a())
executor.submit(b())
print('Done C')


Was This Post Helpful? 0
  • +
  • -

#4 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 4,375
  • Joined: 21-June 11

Re: Async background tasks?

Posted 18 March 2019 - 10:11 AM

executor.submit(a()) will call the function a, which will wait 5 seconds, and then afterwards pass the result of a() (which will be None) as an argument to executor.submit.

You want to pass the actual function to executor.submit, not the result. So that'd be executor.submit(a).
Was This Post Helpful? 1
  • +
  • -

#5 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1364
  • View blog
  • Posts: 4,187
  • Joined: 12-May 09

Re: Async background tasks?

Posted 18 March 2019 - 10:12 AM

Ok, this is neat. So let's examine what you're actually doing.
executor.submit(a())

submit takes a function. You have passed it, however, the result of calling a function.

Imagine this code:
def submit(f):
  f()



If that's what submit does, should you send it a(), or... a?
Was This Post Helpful? 0
  • +
  • -

#6 wujiz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-March 19

Re: Async background tasks?

Posted 18 March 2019 - 12:13 PM

Gotcha! What happens if it's a function of an instance?
class Foo():
    def a(one, two):
        print('A', one, two)
        time.sleep(5)
        print('Done A')


Doing something like executor.submit(Foo.a) works, but not executor.submit(Foo().a). What would I do if I need to call a() of an instance of Foo?
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 4,375
  • Joined: 21-June 11

Re: Async background tasks?

Posted 18 March 2019 - 12:26 PM

I don't see how either of those could work. submit needs a function that can be called without arguments.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1