Anda dapat menerapkan dekorator untuk membuat fungsi Anda tidak sinkron, meskipun itu agak rumit. The multiprocessing
Modul penuh kebiasaan kecil dan pembatasan tampaknya sewenang-wenang - semua alasan untuk merangkum belakang antarmuka yang ramah, meskipun.
from inspect import getmodule
from multiprocessing import Pool
def async(decorated):
r'''Wraps a top-level function around an asynchronous dispatcher.
when the decorated function is called, a task is submitted to a
process pool, and a future object is returned, providing access to an
eventual return value.
The future object has a blocking get() method to access the task
result: it will return immediately if the job is already done, or block
until it completes.
This decorator won't work on methods, due to limitations in Python's
pickling machinery (in principle methods could be made pickleable, but
good luck on that).
'''
# Keeps the original function visible from the module global namespace,
# under a name consistent to its __name__ attribute. This is necessary for
# the multiprocessing pickling machinery to work properly.
module = getmodule(decorated)
decorated.__name__ += '_original'
setattr(module, decorated.__name__, decorated)
def send(*args, **opts):
return async.pool.apply_async(decorated, args, opts)
return send
Kode di bawah ini menggambarkan penggunaan dekorator:
@async
def printsum(uid, values):
summed = 0
for value in values:
summed += value
print("Worker %i: sum value is %i" % (uid, summed))
return (uid, summed)
if __name__ == '__main__':
from random import sample
# The process pool must be created inside __main__.
async.pool = Pool(4)
p = range(0, 1000)
results = []
for i in range(4):
result = printsum(i, sample(p, 100))
results.append(result)
for result in results:
print("Worker %i: sum value is %i" % result.get())
Dalam kasus dunia nyata saya akan menjelaskan sedikit lebih banyak pada dekorator, menyediakan beberapa cara untuk mematikannya untuk debugging (sambil menjaga antarmuka masa depan di tempat), atau mungkin fasilitas untuk berurusan dengan pengecualian; tapi saya pikir ini menunjukkan prinsip dengan cukup baik.
async
danawait
sintaksis dari 3.5).